2

A convention that we have is that, when a merge conflict occurs, after solving it, the original conflicted diff should be added to the commit message, to help developers who may later try to figure out if a bad merge occurred.

The usual way that I do this is

git diff >conflict_log

and then I copy paste this into the commit message afterwards.

There are a few drawbacks to that workflow that I would like to improve:

  • You might forget to save the conflict_log first, and then developers often end up redoing the merge to be able to get the original diff
  • Copy pasting the whole diff may be more difficult if the diff is large and you are working over an ssh session

What I would like to do is make a prepare-commit-msg hook that will automatically add this diff to the commit message.

The problem I have is that at the time that prepare-commit-msg is run, the index has already been changed so git diff works differently and doesn't just show the original conflicts anymore.

I have tried using versions like

git diff hash hash^1 hash^2

And in the documentation here: https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging under "Combined Diff Format" it is suggested to use

git diff --cc

But I'm not sure,

  • should I be using HEAD, MERGE_HEAD, ORIG_HEAD, given that I'm trying to do this in prepare-commit-msg before the merge commit has actually been committed?

  • I want to get the diff that git generates with the <<<<<< markers from conflict resolution, before the user started resolving them, but at the time that prepare-commit-msg runs, the user has already staged their resolutions, is there a way I can take the diff against that state of the index, instead of the current state?

3
  • Possible duplicate of stackoverflow.com/questions/43007243/… (in that both questions are ultimately trying to solve a similar problem) May 5, 2018 at 17:58
  • @RobinGreen I think they are related but it's not a duplicate. I'm specifically trying to write a script that can be used in a prepare-commit-msg hook, and I'm trying to figure out if there's a cleaner way to do it other than stashing the user's changes, trying to restore the index state, then take a diff, then reapply the user's changes, which might all work but is complex and might be brittle in practice. The answer to that question doesn't really give me any leads here.
    – Chris Beck
    May 5, 2018 at 19:33
  • OK, let me spell out what I had in mind then: if you use the approach in that question's top answer whenever you want to solve a bad merge, you won't need to have stored the conflicted merge state, because you can just regenerate it. May 5, 2018 at 21:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.