我最近在我的 git merge 策略上犯了一个错误,直到我已经继续处理该项目并进行了更多提交后才注意到结果错误。为了简洁起见,假设我有两个分支,hotfix
和 new_feature
。我做了类似以下的事情:
git checkout new_feature
git commit -m "Commit to the feature branch"
git checkout hotfix
git pull origin new_feature # Uh oh, I pulled the wrong branch!
git commit -m "Commit to the hotfix"
git push origin hotfix # Uh Oh, pushed the bad merge!
在上述事件之后,其他开发人员也提交了 Hotfix 分支。现在,我们有一个我们想要推出的修补程序,但不能因为它包含未完成的功能工作。
我已经看到很多关于这种情况的类似问题,但没有一个是 100% 匹配的(其中大部分涉及在错误 pull 后直接撤消提交,而不是先推送。我更进一步了,真的无聊的事情。
关于如何解决这个问题的任何分步指南?
谢谢!
编辑:许多其他答案都提到了 git reflog
,所以这里是我的修改副本。我更改了散列和分支名称以匹配我上面的示例。
aaaaaaa HEAD@{0}: checkout: moving from new_feature to hotfix
bbbbbbb HEAD@{1}: checkout: moving from hotfix to new_feature
aaaaaaa HEAD@{2}: commit: Added analytics tracking
ccccccc HEAD@{3}: commit (merge): Fixed a merge issue in compiled css # BAD MERGE?
ddddddd HEAD@{4}: checkout: moving from new_feature to hotfix
eeeeeee HEAD@{5}: commit: Finished updating the events for the header
ddddddd HEAD@{6}: checkout: moving from hotfix to new_feature
ddddddd HEAD@{7}: checkout: moving from new_feature to hotfix
在上面的 reflog 中,HEAD@{3}
看起来是错误的 merge 。我在 hotfix
分支上, pull 入了 new_feature
分支,这导致了 merge 冲突。我修好它们,然后推。我该怎么做才能解决这个问题?
最佳答案
找到提交哈希:git log --pretty=format:"%h %s"| grep “提交修补程序”
还原“Uh oh”提交:git revert 15d2e1f -m 2
关于Git: pull 错了分支,然后推送了 merge 。如何撤消?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17951970/