撤消 git 合并(尚未推送)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5951285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:52:56  来源:igfitidea点击:

Undo a git merge (hasn't been pushed yet)

gitmergerevert

提问by Max Williams

I just committed some changes into one of my feature branches ("feedback_tab") then, checked out "master" and merged them in there. I actually meant to merge them into my "development" branch.

我只是在我的一个功能分支(“feedback_tab”)中提交了一些更改,然后检查了“master”并将它们合并到那里。我实际上打算将它们合并到我的“开发”分支中。

Now, master is ahead of 'origin/master' (its remote) by 17 commits - I haven't pushed the merge up (and don't want to, obviously). How can I revert master back to the same state as before the accidental merge? I'm confused between git revertand git resetwith this stuff.

现在,master 领先于 'origin/master'(它的远程)17 次提交 - 我没有推动合并(显然也不想)。如何将 master 恢复到意外合并之前的状态?我之间困惑git revertgit reset这个东西。

I looked in my git log and there's no entry for merging feedback_tab into master. I'd have thought it would be the top entry?

我查看了我的 git 日志,没有将feedback_tab 合并到master 的条目。我以为它会是顶级条目?

Bit confused :/ any help welcome! max

有点困惑:/欢迎任何帮助!最大限度

采纳答案by Tamás

git reset --hard HEAD~17takes you back 17 commits ahead of the head of master. git rebase -i HEAD~17probably gets rid of the extra commits as well.

git reset --hard HEAD~17带你回到 master 负责人之前的 17 次提交。git rebase -i HEAD~17可能也会摆脱额外的提交。

回答by Hemerson Varela

To undo a merge that was NOT pushed:

要撤消未推送的合并:

git reset --merge ORIG_HEAD

If during the merge you get a conflict, the best way to undo the merge is:

如果在合并期间遇到冲突,撤消合并的最佳方法是:

git merge --abort

回答by Amir Raminfar

Taken from git reset

取自git reset

Undo a merge or pull

    $ git pull                         <1>
    Auto-merging nitfol
    CONFLICT (content): Merge conflict in nitfol
    Automatic merge failed; fix conflicts and then commit the result.
    $ git reset --hard                 <2>
    $ git pull . topic/branch          <3>
    Updating from 41223... to 13134...
    Fast-forward
    $ git reset --hard ORIG_HEAD       <4>

回答by Alon Kogan

This one will surely work!

这个肯定有用!

git reset --hard HEAD~1 
git init

The first one will revert the changes you recently made (the merge) the second will init the repo to latest (therefore will fast forward to latest on origin)

第一个将恢复您最近所做的更改(合并),第二个将把 repo 初始化为最新的(因此将快进到最新的原点)

I've tried

我试过了

git reset --merge

but it didn't do the trick.

但它没有成功。