对这篇文章的试验: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)
step1:
git branch revert-branch HEAD^ (新建branch,指向HEAD的前一次commit:3c21c9c)
step2:
git checkout revert-branch(移动HEAD指针到新的branch,同时用HEAD所指的commit覆盖index及working directory)
注:以上两步可以合并成一步git checkout -b revert-branch HEAD^
step3:
git reset --soft master(移动当前branch的head及HEAD到master所指的位置,index及working dir保持不变)
此时执行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)
step 5:
git checkout master(移动HEAD指针到master分支)
step 6:
git merge revert-branch(执行fast-forward merge)
Let's verify:
Note:
git reset --soft|--mixed|--hard commitID
如果对文中提及的概念不是很清楚的, 请看这里: http://www.cnblogs.com/kidsitcn/p/4513297.html