➜ mygit git:(master) vim test.txt ➜ mygit git:(master) ✗ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
➜ mygit git:(master) vim test.txt ➜ mygit git:(master) ✗ git add test.txt ➜ mygit git:(master) ✗ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
modified: test.txt
➜ mygit git:(master) ✗ git reset HEAD test.txt Unstaged changes after reset: M test.txt
这次编辑完后将文件添加到暂存区,git status其实我们可以执行git reset HEAD <file>将文件从暂存区移除。
➜ mygit git:(master) git checkout ca07f7b Note: checking out 'ca07f7b'.
You are in'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:
➜ mygit git:(ca07f7b) vi test.txt ➜ mygit git:(ca07f7b) ✗ git status HEAD detached at ca07f7b Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a") ➜ mygit git:(ca07f7b) ✗ git checkout master error: Your local changes to the following files would be overwritten by checkout: test.txt Please commit your changes or stash them before you switch branches. Aborting
➜ mygit git:(test) vi test.txt ➜ mygit git:(test) ✗ git status On branch test Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a") ➜ mygit git:(test) ✗ git checkout master error: Your local changes to the following files would be overwritten by checkout: test.txt Please commit your changes or stash them before you switch branches. Aborting
➜ mygit git:(test) ✗ git stash Saved working directory and index state WIP on test: c96bc8f commit
将当前的工作区保存,现在再切换分支,就没有问题了。
1 2 3 4 5 6 7 8 9
➜ mygit git:(test) ✗ git stash Saved working directory and index state WIP on test: c96bc8f commit ➜ mygit git:(test) git status On branch test nothing to commit, working tree clean ➜ mygit git:(test) git checkout - Switched to branch 'master' ➜ mygit git:(master) git checkout - Switched to branch 'test'
现在我们要恢复之前的修改,可以用git stash list查看已经保存的列表。
1 2 3 4
➜ mygit git:(test) git stash list stash@{0}: WIP on test: c96bc8f commit (END)
多保存一次:
1 2 3 4 5 6 7
➜ mygit git:(test) vi test.txt ➜ mygit git:(test) ✗ git stash save 'hello' Saved working directory and index state On test: hello ➜ mygit git:(test) git stash list stash@{0}: On test: hello stash@{1}: WIP on test: c96bc8f commit (END)
恢复:
1 2 3 4 5 6 7 8 9
➜ mygit git:(test) git stash pop ➜ mygit git:(test) git stash apply Auto-merging test.txt CONFLICT (content): Merge conflict in test.txt ➜ mygit git:(test) ✗ vi test.txt ➜ mygit git:(test) ✗ git stash drop stash@{1} stash@{1} is not a valid reference ➜ mygit git:(test) ✗ git stash drop stash@{0} Dropped stash@{0} (62dec20aca499f2e2660eb106fae481607d3662f)
保存现场
git stash
git stash list
恢复现场
git statsh apply (stash内容并不删除,需要通过git statsh drop stash@{0}手动删除)
git stash pop(恢复的同时也将stashn内容删除)
git stash apply stash@{0}
标签
标签是版本库的一个快照,一般我们在发版的时候会用到。
新建标签
标签有两种:
轻量级标签(lightweight)git tag v1.0.1
带有附注标签(annotated)git tag -a v1.0.2 0m 'release version'
1 2 3 4 5
➜ mygit git:(test) git tag v1.0 ➜ mygit git:(test) git tag -a v2.0 -m '2.0 released' ➜ mygit git:(test) git tag v1.0 v2.0
查找
git tag -l 'pattern'
1 2 3 4 5
➜ mygit git:(test) git tag -l 'v1.0' v1.0 ➜ mygit git:(test) git tag -l 'v*' v1.0 v2.0