git – What is the best way to unmerge staged and modfy elements with conflicts?

Question:

I did an unnecessary merge with a branch, now I have several elements staged and not merged because there are conflicts. What's the best way to reverse this merge and leave the branch on the last commit before this unwanted action?

Answer:

Assuming the merge didn't generate any commits (since there are conflicts). At the root of your repository, type

git reset .

This will remove all changes that are staged , putting them back in the modified list.

Then reverse all changes:

git reset HEAD --hard

This will revert all modifications to the modified files. Note that if you had any other modification, it will also be lost.

If merge added new files to the repository, then you can "clean" them up via git clean . Again from the root of the repository:

git clean -xdf

As always, be careful when using git clean as it may delete files you'd like to have. You can use the n option instead of f to list the files that will be deleted.

Scroll to Top