views:

3365

answers:

6
+7  Q: 

git: undo a merge?

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 the instructions I found on this website, I tried entering the command 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, such that doing a git push will do nothing. How do I get back to that point?

A: 

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

NebuSoft
I tried `git reset`, `git reset --merge`, and `git reset --hard`, but I still end up with the same message about being 5 commits ahead of origin/master.
Matt Huggins
+4  A: 

You should reset to previous commit, this should work:

git reset --hard HEAD^

or even HEAD^^ to revert that revert commit. You can always give full sha ref if you're not sure how many steps back you should take.

In case when you have problems and your master branch didn't had any local changes, you can reset to origin/master.

MBO
Now it says: `# On branch master``# Your branch and 'origin/master' have diverged,``# and have 3 and 3 different commit(s) each, respectively.`
Matt Huggins
+15  A: 

With git log check which commit is one prior the merge. Then you can reset it using:

git reset --hard commit_sha

There's also another way

git reset --hard HEAD~5

will get you back 5 commits.

Marcin Gil
+2  A: 

Okay, the answers other people here gave me were close, but didn't work. Here's what I did.

Doing this...

git reset --hard HEAD^
git status

...gave me the following status.

# On branch master
# Your branch and 'origin/master' have diverged,
# and have 3 and 3 different commit(s) each, respectively.

I then had to type in the same git reset command several more times. Each time I did that, the message changed by one as you can see below.

> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 3 and 3 different commit(s) each, respectively.
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 2 and 3 different commit(s) each, respectively.
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 1 and 3 different commit(s) each, respectively.
> git reset --hard HEAD^
HEAD is now at [...truncated...]
> git status
# On branch master
# Your branch is behind 'origin/master' by 3 commits, and can be fast-forwarded.

At this point, I saw the status message changed, so I tried doing a git pull, and that seemed to work:

> git pull
Updating 2df6af4..12bbd2f
Fast forward
 app/views/truncated |    9 ++++++---
 app/views/truncated |   13 +++++++++++++
 app/views/truncated |    2 +-
 3 files changed, 20 insertions(+), 4 deletions(-)
> git status
# On branch master

So long story short, my commands came down to this:

git reset --hard HEAD^
git reset --hard HEAD^
git reset --hard HEAD^
git reset --hard HEAD^
git pull
Matt Huggins
or you could've used `HEAD^^^^`
hasen j
maybe even reset to `origin/master` ;)
hasen j
Yeah, I'm still learning, sorry. :) I'll get it down...just realearning everything I already knew in Subversion!
Matt Huggins
+1  A: 

Fresh: http://progit.org/2010/03/02/undoing-merges.html

peritus
A: 

I think you can do git rebase -i [hash] [branch_name] where [hash] is the identifying hash for however far back you want to rewind plus one (or however many commits back you want to go) and then delete the lines for the commits in the editor that you don't want any more. Save the file. Exit. Pray. And it should be rewound. You might have to do a git reset --hard, but it should be good at this point. You can also use this to pull specific commits out of a stack, if you don't want to keep them in your history, but that can leave your repository in a state that you probably don't want.

tychoish