2

I have merged master into my bugXY branch, and needed to merge a file. But git mergetool failed (my noob fault, I guess), and even though I aborted the merge it left me with a dirty working directory but not in need-to-merge state. I would like to repeat the merge command, but I can't due to some new files left by the merge.

How can I undo the merge (I didn't commit anything) and get exactly the working directory from before?

What I did:

> git checkout bugXY
On branch "bugXY" - nothing to commit
> git merge master
[modifying and adding lots of files]
one file with conflict
On branch "bugXY|MERGING"
> git mergetool
You could use a, b or c
merged // didn't work, no GUI appeared and I did not do anything
do you want to remove, commit or abort?
> abort // Hu, what happened?
On branch "bugXY" // no MERGING any more???

Oops, what have I done? A repeated git merge does not work, I have changed (and staged) files in my working directory now. OK, undo it. I tried:

> git reset --hard HEAD

and

> git checkout bugXY

Now nothing is staged any more, but I still have a bunch of untracked working tree files which hinder me to git merge again ("would be overwritten, please [re]move them"). How can I do that? I guess rm path/to/file.php for each of them would work, but there are a lot of them...

3 Answers 3

15

Show files that git would purge if you weren't doing a dry run:

git clean -ndx

Really purge all those files from your working tree:

git clean -fdx
2
  • 1
    Be careful when running the second command...or prey your IDE has a local history feature
    – Carlton
    Commented Apr 25, 2016 at 12:08
  • Also be careful running this because it removes all files that aren't being tracked, including ignored files. Commented Oct 24, 2018 at 15:11
9

git merge manual says:

--abort

Abort the current conflict resolution process, and try to reconstruct the pre-merge state.

So I'd try to git merge --abort to start afresh.

1
  • I tried that, but it told me I were no more in a MERGING situation. Thanks anyway
    – Bergi
    Commented Jun 14, 2012 at 14:52
3

git reset --hard HEAD Resets the index and working tree. Any changes to tracked files in the working tree since are discarded.

If you didn't commit the files before you ran the reset command, then they get wiped out by the reset command. One of the options you have would be to use git reset --hard which will reset you back to the state of the provided commit.

Hopefully that's helpful to you.

1
  • I tried that already. But something I did before had left them untracked :-(
    – Bergi
    Commented Jun 14, 2012 at 8:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.