174

I am working on a project using Git as the VCS. I got a branch xyz cut from the mainline branch of master. After working for a while, I committed my code and took a pull of the branch mainline.

The pull was fine. I then merged the code with master. After the merge, there were problems in certain files. I have not committed the code after merging. Can someone please guide as to how I can abort this merge and bring my branch where I am working currently to the state where it was before I merged it?

Salman Kazmi
  • 2,101
  • 2
  • 16
  • 28
  • 1
    Possible duplicate of [How to revert Git repository to a previous commit?](http://stackoverflow.com/questions/4114095/how-to-revert-git-repository-to-a-previous-commit) – mttdbrd May 18 '17 at 13:24
  • Can't you just restore the previous commit of the branch? – Wutipong Wongsakuldej May 18 '17 at 13:25
  • 2
    Possible duplicate of [How to undo a git merge with conflicts](http://stackoverflow.com/questions/5741407/how-to-undo-a-git-merge-with-conflicts) – Scott Weldon May 18 '17 at 16:40
  • 1
    Note that in Git, `revert` is the name of a command that creates a *new commit* reverting the changes in a previous commit, so that is probably not the word you are looking for here. I have [edited](//stackoverflow.com/help/editing) your question to clarify. – Scott Weldon May 18 '17 at 16:48

3 Answers3

404

as long as you did not commit you can type

git merge --abort

just as the command line suggested.

Timothy Truckle
  • 12,232
  • 2
  • 22
  • 44
  • 9
    I got this message when trying this: `error: Entry 'MyFile.java' not uptodate. Cannot merge. fatal: Could not reset index file to revision 'HEAD'.` – SMBiggs Feb 21 '19 at 23:58
  • 1
    @Ahmad git does not start a merge with uncommitted changes (of already tracked files) in place. git cannot protect what it doesn't know. – Timothy Truckle Apr 02 '19 at 16:35
  • yeah you are i learnt it by losing my code. here is detail of my problem , how i fell in problem. https://stackoverflow.com/questions/55474092/how-to-undo-the-git-abort – Ahmad Apr 02 '19 at 16:53
  • Made my day <3 thanks – David 'mArm' Ansermot Mar 08 '21 at 12:39
26

If you do "git status" while having a merge conflict, the first thing git shows you is how to abort the merge.

output of git status while having a merge conflict

Stephen Rauch
  • 40,722
  • 30
  • 82
  • 105
magician
  • 377
  • 3
  • 7
  • Nice, this suggested me to `git reset HEAD ` which effectively removed the "merge" collapsable from the git extension source control => then I could either resolve the conflicts or just revert my changes in the GIT extension list :) – jave.web Jul 09 '20 at 13:47
11

Truth be told there are many, many resources explaining how to do this already out on the web:

Git: how to reverse-merge a commit?

Git: how to reverse-merge a commit?

Undoing Merges, from Git's blog (retrieved from archive.org's Wayback Machine)

So I guess I'll just summarize some of these:

  1. git revert <merge commit hash>
    This creates an extra "revert" commit saying you undid a merge

  2. git reset --hard <commit hash *before* the merge>
    This reset history to before you did the merge. If you have commits after the merge you will need to cherry-pick them on to afterwards.

But honestly this guide here is better than anything I can explain, with diagrams! :)

denis.lobanov
  • 309
  • 1
  • 8