Git:拉取了错误的分支,然后推送了合并。如何撤销?

6

我最近在我的git合并策略中犯了一个错误,直到我已经继续在项目上工作并提交了更多的提交后才注意到结果的错误。为了简洁起见,让我们假设我有两个分支,hotfixnew_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分支进行了提交。现在,我们有一个想要推出的热修复,但由于包含未完成的功能工作,无法推送。
我看过很多类似的问题,但没有完全匹配的(大多数涉及直接撤消错误拉取后的提交,而不是首先推送。我更进一步,真的搞砸了。
有没有逐步指南,可以帮助我解决这个问题?
谢谢!
编辑:许多其他答案提到了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} 看起来是有问题的合并。我当时在 hotfix 分支上,并合并了 new_feature 分支,导致出现了合并冲突。我解决了这些冲突并进行了推送。那么我应该怎么做才能修复这个问题呢?
1个回答

3

找到提交哈希值:git log --pretty=format:"%h %s" | grep "Commit to the hotfix"

撤销“Uh oh”提交:git revert 15d2e1f -m 2


1
但是这不仅仅是一个提交。 "Uh Oh" 提交是从另一个分支合并而来的,其中包括一堆提交!我需要查看 Hotfix 历史记录并撤销一堆提交,这似乎有点“hacky”。 - Mike Trpcic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接