Question

Assume I did a rebase and 100 files have conflicts. I have to resolve them one by one. Let us say I have resolved (N-1) files and I am working on the file N. After working on it for some time, I find I mess up the file. So I want to resolve it from scratch again. But I don't want to abort the rebase and do rebase again as I don't want to resolve the (N-1) files again.

Is it possible to recover the merge conflict for the file N only so I can resolve it again from scratch?

Was it helpful?

Solution

Assuming the file you are working on is named test.txt, and that the file had been left with conflict markers due to the merge attempt, and you've somehow botched doing the manual resolution of those conflicts, you can re-create the file with conflict markers with a few commands.

As background, it helps to know that, when a merge needs manual conflict resolution on a file, it leaves three different copies (called "stages") of that file in your git index. Stage 1 is a common ancestor of the two versions of the file being merged, and stages 2 and 3 are the two versions from the two branches you are trying to merge. Some, but not all, of the git utilities understand the syntax :<stage>:<filename> to reference these entries.

So, what you need to do first is to re-create temporary copies of these three files somewhere - I'll use /tmp here, but it's not mandatory:

git cat-file -p :1:test.txt > /tmp/test.txt.1
git cat-file -p :2:test.txt > /tmp/test.txt.2
git cat-file -p :3:test.txt > /tmp/test.txt.3

Then, use the git plumbing command git merge-file to recreate a file with the appropriate conflict markers. Note that the order of arguments is important here, and that it would probably be a good idea to save what work you've already done on this file in case you want to reference it in re-doing your merging.

mv test.txt test.txt.broken
git merge-file -p /tmp/test.txt.2 /tmp/test.txt.1 /tmp/test.txt.3 > test.txt

This will re-create test.txt with the conflict markers (although without the branch name comments that are typically included - if you really want those, you'll need to add some -L <branchname> arguments - you can type git help merge-file to get more information on that).

At that point, you can clean up the temporary files, and start over on resolving the conflicts in that file. Remember to git add it when you're done, and then continue with the rest of the files.

OTHER TIPS

I believe you're looking for git checkout --merge --path/to/file.

Maybe not so good a solution (looks like not so git'ish), but here is one possible way:

  1. copy the (N - 1) files to a temp dir
  2. git rebase --abort
  3. copy the (N - 1) files back the git dir
  4. work on your Nth file.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top