git remove files when undoing failed merge

10,469

Solution 1

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

Solution 2

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.

Solution 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.

Share:
10,469
Bergi
Author by

Bergi

ecmascript-6 and promise evangelist.

Updated on June 14, 2022

Comments

  • Bergi
    Bergi almost 2 years

    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...