如何回滚前两次提交?

想想这个场景:

  1. 开发人员 A 提交: # n
  2. Dev B 确实犯了 # n + 1
  3. Dev A 确实犯了 # n + 2
  4. 然后提交 # n + 3

然后发现在他的提交 # n + 2中引入了一个缺陷。

如何开发。回滚他的最后2次提交,并继续开发提交 # n + 1

尝试了 git reset --hard HEAD~2*,但是返回到 dev A 的提交 # n

138597 次浏览

It should come back to the n+1 commit. You probably have a merge commit in there as well. You can also do a git reset --hard <sha1_of_where_you_want_to_be>

WARNING!! --hard means that any uncommitted changes you currently have will be thrown away permanently.

I would suggest using instead something like this:

git reset --soft <commit_hash>

Unless you want it to remove all the changes up to that point, in that case use --hard instead of --soft, it would get you to the desired point in the tree WITHOUT trowing away all of the changes made in the commits.

While I was reading I tried with --HARD and for my misfortune, it removed all my changes up to that point. So, pay attention to whether you want them removed or not.

so, if you are unlucky as me try this! : git revert <commit_hash>