Git跟SVN最大不同的地方就是分布式。SVN的集中式与Git的分布式决定各自的业务场景。既然是分布式的,那么大部分操作就是本地操作。一般Git操作都是通过IDE,比如Eclipse,如果装了Git Bash,也可以通过命令行操作(不建议使用Git Bash的界面操作)。Git Bash下载地址:https://www.git-scm.com/download,打开后点右边的Downloader XXX for Windows,选择适合自己操作系统的版本下载。
选择安装目录,一路默认过去,安装后就可以使用Git Bash了,下面列出常用的命令:
1. 下载(git clone):从github上通过代码路径下载到本地
wulf@wulf00 MINGW64 ~ $ git clone ssh://git@gitlab.cmread.com:2022/newportal/ms-activity-service.git Cloning into 'ms-activity-service'... Warning: Permanently added the RSA host key for IP address '[112.13.170.217]:2022' to the list of known hosts. remote: Counting objects: 377, done. remote: Compressing objects: 100% (215/215), done. remote: Total 377 (delta 165), reused 328 (delta 127) Receiving objects: 100% (377/377), 182.48 KiB | 6.76 MiB/s, done. Resolving deltas: 100% (165/165), done.
2. 查看工作区的文件状态(git status):先进入下载到本地的git工作仓库,查看状态
wulf@wulf00 MINGW64 ~ $ cd C:/Users/wulf/ms-activity/ wulf@wulf00 MINGW64 ~/ms-activity (master) $ git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track)
3. 将新增的文件或者修改过的文件加入暂存区(git add):先创建一个文件,然后添加文件到暂存区,最后查看工作区状态:
wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master) $ vi test.xml wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master) $ git add test.xml warning: LF will be replaced by CRLF in test-pilling/src/test/resources/test.xml. The file will have its original line endings in your working directory. wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master) $ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: test.xml Untracked files: (use "git add <file>..." to include in what will be committed) ../../../../.project ../../../../.settings/ ../../../../memcache-view/.classpath ../../../../memcache-view/.gitignore ../../../../memcache-view/.project ../../../../memcache-view/.settings/ ../../../.classpath ../../../.gitignore ../../../.project ../../../.settings/ wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master)
4. git add . (将当前工作区的所有修改过的文件加入暂存区):这个.代表当前工作区,批量操作
5. 将暂存区的文件提交到本地Git仓库(git commit):这里提交的是本地仓库,不是远程仓库
wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master) $ git commit -m "test commit" [master 739bfa3] test commit Committer: Jimmy Wu(吴林锋) <wulf@inspur.com> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly. Run the following command and follow the instructions in your editor to edit your configuration file: git config --global --edit After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 13 insertions(+) create mode 100644 test-pilling/src/test/resources/test.xml
6. 将本地Git仓库的文件提交到远程仓库(git push):这一步才是提交到远程仓库
7. 将远程的文件更新到本地仓库 (git fetch)
8. 将本地仓库的文件合并到当前工作区 (git merge)
9. 将远程的文件更新到本地仓库,然后合并到当前工作区 (git pull):git pull = git fetch + git merge
wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master)
$ git pull
Already up to date.
10. 创建文支 (git branch)
11. 切换分支、覆盖文件 (git checkout):git checkout即可切换分支(参数是分支名),也可恢复文件(参数是文件名),比如我修改了test.xml并commit了,后面又决定不改了
wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (test-branch)
$ git checkout test.xml
12. 在当前分支创建新的分支(git checkout -b):git checkout –b test-branch = git branch test-branch + git checkout test-branch
wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master) $ git checkout -b test-branch Switched to a new branch 'test-branch'
13. 将test-branch分支合并到主干分支(master)上:先切换(合入哪里先切换到哪里),再合入
wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (test-branch) $ git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master) $ git merge test-branch Already up to date.
14. 删除分支:
wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master) $ git branch -d test-branch Deleted branch test-branch (was 739bfa3).
15. 删除文件(git rm):删除暂存区
或分支
上的文件, 同时工作区也不需要这个文件了,如果本地还想保留文件,需要加上参数cached
wulf@wulf00 MINGW64 /e/workspace/Wireless-Router/test-pilling/src/test/resources (master) $ git rm test.xml rm 'test-pilling/src/test/resources/test.xml'
下面是已经git add到暂存区,想从暂存区删掉.classpath这个文件,又不想commit到本地仓库了,同时不想删掉本地这个文件
wulf@wulf00 MINGW64 /e/workspace/TeaPot (master) $ git rm --cached .classpath rm '.classpath'
16.加入不想提交的文件列表:在当前仓库目录下编辑.gitignore文件
wulf@wulf00 MINGW64 /e/workspace/TeaPot (master)
$ vi .gitignore
wulf@wulf00 MINGW64 /e/workspace/TeaPot (master) $ cat .gitignore /target/ .gitignore .project dependency-reduced-pom.xml /.settings/ /bin/
以上文件列表都不会被git add到暂存区。