57

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.

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 revert and git reset with this stuff.

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?

Bit confused :/ any help welcome! max

Andrew C
  • 13,845
  • 6
  • 50
  • 57
Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • @myself: Oh dear, oh dear, should've read the faq: "software tools commonly used by programmers". – orlp May 10 '11 at 14:07

4 Answers4

91

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
Hemerson Varela
  • 24,034
  • 16
  • 68
  • 69
37

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

Tamás
  • 47,239
  • 12
  • 105
  • 124
  • 26
    You might want to use `reset --merge`. That does the same thing if the merge was the only thing that happened, but if you had local modifications (to files unaffected by the merge) it leaves them in place instead of destroying them like `--hard` does. – Cascabel May 10 '11 at 20:19
  • 17
    Could also do `git reset --hard origin/master` – Karl May 12 '11 at 00:16
  • @Jefromi - ah right, will bear that in mind next time. Thanks! – Max Williams May 12 '11 at 09:35
  • @Karl - i take it that just says "make current branch a copy of origin/master' then? – Max Williams May 12 '11 at 09:36
  • @Max: I would say "make current branch point to the same commit as origin/master (and drag the index and work tree along with it)". But yes, that's pretty much a copy. – Cascabel May 12 '11 at 11:10
12

Taken from 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>
Amir Raminfar
  • 33,777
  • 7
  • 93
  • 123
5

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)

I've tried

git reset --merge

but it didn't do the trick.

Alon Kogan
  • 3,258
  • 1
  • 21
  • 20