SeattleOrBayArea
SeattleOrBayArea

Reputation: 3118

Git merging a file, actually trying to re-do the merge for one file

I am merging a branch to my upstream branch. You can consider it to be just a merge between two branches. Now, after I took care of all the merge conflicts manually, I did a fresh build and found that one of the source files has compilation errors.

To solve this, I wanted to re-do the merge for this file with errors without doing it for the rest of the files I already merged. So, is there a way to fix this without taking the pain of doing it from scratch.

To explain it better, assume three files A, B and C are present in the upstream. I run git pull to get the content from remote branch to be merged with these. Now, I see merge conflicts and I resolve these manually, file by file. On doing compilation, I see file A and C compile fine whereas B has errors. So, now I want to re-merge B from start without wasting the effort I put in merging A and C.

Upvotes: 2

Views: 902

Answers (3)

Adam Dymitruk
Adam Dymitruk

Reputation: 129584

Sounds like the merge resulted in the solution not ending up in a good state. Just edit the files you need to change so everything compiles and all your tests pass. If you want certain files to be pulled off of a state just before the merge,

git checkout origin/branch_you_want -- fileA fileB

Now just amend your merge commit:

git add -A
git commit --amend -C HEAD

This will reuse the same message. You should see your merge commit in a good state now.

UPDATE:

WARNING! When doing merges and using --amend, you may have recorded a conflict resolution you did not intend! See rerere: http://progit.org/2010/03/08/rerere.html

Upvotes: 1

Cascabel
Cascabel

Reputation: 496922

Since you haven't committed it yet, you can just check out the unmerged version of the file:

git checkout --merge path/to/file

Then you can re-resolve the conflict as you like. (-m is a synonym for --merge.)

Upvotes: 3

Lily Ballard
Lily Ballard

Reputation: 185681

You can run git merge-file, which does a 3-way merge. If HEAD is the merge you just did, something like the following should work:

git cat-file blob HEAD^:fileB > fileB # revert fileB to its pre-merge condition
git cat-file blob $(git merge-base HEAD^ HEAD^2):fileB > fileB.base # write the base version to fileB.base
git cat-file blob HEAD^2:fileB > fileB.other # write the other version of fileB
git merge-file fileB fileB.base fileB.other

This will re-run the merge and write the merge results (including conflicts) back into fileB (at which point you can delete fileB.base and fileB.other).

Edit:

If you haven't actually ran git add fileB yet (e.g. if git status shows the merge conflict), and if you have the git-merge-one-file script on your disk (in my case it's sitting at /usr/libexec/git-core/git-merge-one-file), then you can run git merge-index /path/to/git-merge-one-file fileB and it will re-merge that file for you.

Upvotes: 3

Related Questions