Git 技巧:恢复到前一次提交 (图示分析)

对这篇文章的试验:http://www.oschina.net/translate/git-tips-revert-with-new-commit, FYI
prerequisites:
git init test
cd test
touch first.txt
git add .
git commit -m "intial commit." (生成commit id: 3c21c9c)
touch second.txt
git add.
git commit -m "2nd commit.",(生成commit id: 3c55cf4)
Git 技巧:恢复到前一次提交 (图示分析)


step1:
git branch revert-branch HEAD^ (新建branch,指向HEAD的前一次commit:3c21c9c)
Git 技巧:恢复到前一次提交 (图示分析)
step2:
git checkout revert-branch(移动HEAD指针到新的branch,同时用HEAD所指的commit覆盖index及working directory)
注:以上两步可以合并成一步git checkout -b revert-branch HEAD^
Git 技巧:恢复到前一次提交 (图示分析)

step3:
git reset --soft master(移动当前branch的head及HEAD到master所指的位置,index及working dir保持不变)

Git 技巧:恢复到前一次提交 (图示分析)
此时执行git status会看到如下结果:

$ git status
# On branch revert-branch
# Changes to be committed:
#   (use  to unstage)
#
#       deleted:    second.txt
#

step4:
git commit -m "reverted to initial state."(提交index/staging area的数据到commit history)

Git 技巧:恢复到前一次提交 (图示分析)
step 5:
git checkout master(移动HEAD指针到master分支)

Git 技巧:恢复到前一次提交 (图示分析)
step 6:
git merge revert-branch(执行fast-forward merge)
Git 技巧:恢复到前一次提交 (图示分析)

Let's verify:


Note:
git reset --soft|--mixed|--hard commitID
Git 技巧:恢复到前一次提交 (图示分析)

你可能感兴趣的:(git,reset)