提交图表(如何):
* 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
...
答案 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
中所做更改的撤消