Undo a Git merge that hasn't been pushed yet

2018-12-31 03:48发布

Within my master branch, I did a git merge some-other-branch locally, but never pushed the changes to origin master. I didn't mean to merge, so I'd like to undo it. When doing a git status after my merge, I was getting this message:

# On branch master
# Your branch is ahead of 'origin/master' by 4 commits.

Based upon some instructions I found, I tried running

git revert HEAD -m 1

but now I'm getting this message with git status:

# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.

I don't want my branch to be ahead by any number of commits. How do I get back to that point?

27条回答
公子世无双
2楼-- · 2018-12-31 03:58

The simplest answer is the one given by odinho - Velmont

First do git reset --merge ORIG_HEAD

For those looking to reset after changes are pushed, do this (Because this is the first post seen for any git reset merge questions)

git push origin HEAD --force

This will reset in a way that you won't get the merged changes back again after pull.

查看更多
只靠听说
3楼-- · 2018-12-31 03:59

It is strange that the simplest command was missing. Most answers work, but undoing the merge you just did, this is the easy and safe way:

git reset --merge ORIG_HEAD

The ref ORIG_HEAD will point to the original commit from before the merge.

(The --merge option has nothing to do with the merge. It's just like git reset --hard ORIG_HEAD, but safer since it doesn't touch uncommitted changes.)

查看更多
泛滥B
4楼-- · 2018-12-31 04:01

Assuming your local master was not ahead of origin/master, you should be able to do

git reset --hard origin/master

Then your local master branch should look identical to origin/master.

查看更多
看淡一切
5楼-- · 2018-12-31 04:01

If your merge and the corresponding commits were not pushed yet, you can always switch to another branch, delete the original one and re-create it.

For example, I accidentally merged a develop branch into master and wanted to undo that. Using the following steps:

git checkout develop
git branch -D master
git branch -t master origin/master

Voila! Master is at the same stage as origin, and your mis-merged state is erased.

查看更多
旧人旧事旧时光
6楼-- · 2018-12-31 04:01

You can use the git-reset command.

git-reset - Reset current HEAD to the

specified state. git reset [--mixed |

--soft | --hard | --merge] [-q] [] git reset [-q] []

[--] … git reset --patch

[] [--] […]

GIT-Reset

查看更多
像晚风撩人
7楼-- · 2018-12-31 04:02

With modern Git, you can:

git merge --abort

Older syntax:

git reset --merge

Old-school:

git reset --hard

But actually, it is worth noticing that git merge --abort is only equivalent to git reset --merge given that MERGE_HEAD is present. This can be read in the Git help for merge command.

git merge --abort is equivalent to git reset --merge when MERGE_HEAD is present.

After a failed merge, when there is no MERGE_HEAD, the failed merge can be undone with git reset --merge, but not necessarily with git merge --abort, so they are not only old and new syntax for the same thing.

Personally I find git reset --merge much more powerful and useful in everyday work, so that's the one I always use.

查看更多
登录 后发表回答