Git:拉错了分支,然后推送了合并.如何撤消



我最近在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分支进行了提交。现在,我们有一个修补程序,我们想推出,但不能,因为它包含尚未完成的功能。

我看到过很多关于这种情况的类似问题,但没有一个是100%匹配的(其中大多数涉及在糟糕的拉动后直接取消提交,而不是先推。我更进一步,真的很糟糕。

有关于我如何补救的分步指南吗?

谢谢!

编辑:许多其他答案都提到了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分支,这导致了合并冲突。我把它们修好,推了推。我该怎么办才能解决这个问题?

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

还原"哦"提交:git revert 15d2e1f -m 2

最新更新