lapinkoira
lapinkoira

Reputation: 8978

Git undo merge and add for a single file

Having merged a branch I got some merge conflicts.

I fixed them and for every file I fixed I added it with git add to the merge commit.

How could I undo a git add of one of these files if I realized later by running the code the merge is wrong? all this before commiting anything, the files are still added and modified and uncommited.

Upvotes: 2

Views: 1973

Answers (2)

dubes
dubes

Reputation: 5514

If you want to undo git add

With git add you have staged a file. You can use git reset with the path to file in order to unstage that particular file.

git reset <filePath>

From the official documentation:

This means that git reset is the opposite of git add

If you want to correct your merge

Since you have not committed the merged file, you can still make changes to it (assuming you know which changes need to be made). You can then continue with adding and committing the file as usual.

Edit : Adding the correct answer above for completeness of this answer.

If you would like to "undo" the merge after you have applied it and before you have committed it, you can use the -m flag on git checkout

git checkout -m <file>

Upvotes: 3

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59915

I would like to have again the conflict in that file, to reset it somehow, to have <<<<<<< and >>>>>

You can restore the merge conflict markers in your working directory by using the --conflict option of git-checkout:

git checkout --conflict=merge -- path/to/file

From the documentation:

This will re-checkout the file again and replace the merge conflict markers. This can be useful if you want to reset the markers and try to resolve them again.

Upvotes: 6

Related Questions