撤消合并分支中的提交

时间:2015-05-31 23:23:46

标签: git

提交图表(如何):

* hash1 - (HEAD) last commit
* hash2 - merge commit
|\
| * hash3 - last branch commit that should be undone
| * hash4 - this commit should have been merged instead of hash3
| * hash5 - first branch commit
* | hash6 - commit on master after branch creation
|/
* hash7 - before trouble began
...

提交hash4是最后一个工作提交,但是在不同的分支(不是主)上。 hash3是一个错误的提交,它会破坏项目,但会与master(hash2)合并。 hash1尝试修复是不成功的。如何恢复hash4并与hash6合并以再次获得有效的项目?

提交图表(应该如何):

* hash2 - merge commit
|\
| * hash4 - this commit should have been merged instead of hash3
| * hash5 - first branch commit
* | hash6 - commit on master after branch creation
|/
* hash7 - before trouble began
...

1 个答案:

答案 0 :(得分:0)

您有几种选择:

<强> git reflog

reflog存储对HEAD所做的每项更改的历史记录。您可以将存储库签出到任何给定的提交(分离的HEAD),从该点创建分支并执行您的工作。

例如:

git reflog
.. find out the HEAD@{n} of your desired commit
git checkout HEAD@{n}
git checkout -b my_new_branch
// At this point you have new branch with **head4** as your last commit

<强> git revert

git revert让你“回滚”=恢复你想要丢弃的提交。

git revert SHA1 SHA1 SHA1 SHA1

git revert head3

这将创建一个提交,其中包含对head3

中所做更改的撤消