1

I'm attempting to merge a long-standing branch, and not surprisingly, there's a fair number of merge conflicts which need to get resolved. I've worked through a fair number of them, git-add-ing each file once the conflicts have been resolved.

However, I now realize that for some of the conflicts, resolution would be easier if I could temporarily go back to the pre-merge state, fix up the files, and then re-do the merge, at least for those files. (Specifically, part of the conflicts has to do with an automatic code formatting tool. I'd like to be able to run that same tool on the branch pre-merge, so that merging only has to deal with the non-auto-formatting changes.)

I know I can drop the merge entirely, but I have a large number of manual resolutions which I don't want to lose. Is there any way to save the manual resolutions such that they can be re-applied after I redo the merge?

R.M.
  • 3,461
  • 1
  • 21
  • 41

1 Answers1

1

There's only one way to save things permanently, or semi-permanently, in Git, and that is to commit them. (That's what git stash does: it makes commits. It just makes them on no branch.)

Unfortunately, you cannot commit with a partially merged index. (More precisely, if any index entries are in stages 1, 2, or 3, you cannot commit.)

There are several workarounds. None of them are great. Probably the best one for your situation is to use git worktree (if your Git is at least 2.6—the feature went in to Git 2.5 but I vaguely recall some major bug fix in the next update). The main constraint on this is that each work-tree must be in its own unique branch.

Using git worktree add will allow you do to what you want because each added work-tree gets its own index, so that you can get those pre-merge states, and make fixes and merge files and commit the results. You don't even have to commit the results, since once you are done, you can copy the merged result over to your "main" work-tree and then simply discard the added work-tree. But you do have to work in a separate branch (just start the new branch out from the same commit you're working on in the main work-tree).

See also temporary commit on complex merge.

torek
  • 448,244
  • 59
  • 642
  • 775