19

Is there a git hook I can use for merge conflicts? After a failed git merge, it would be great to be able to write a script that opens all files with conflicts in $EDITOR. Unfortunately the post-merge hook doesn't run if there are conflicts and from what I've seen in the githooks man page, there are no other applicable hooks.

I'm wondering if I've missed something, or if there are other alternatives short of aliasing 'git merge' to a function or something like that.

Thanks, Chris

flooose
  • 499
  • 8
  • 25
  • 1
    You could use a custom mergetool that is just an editor rather than a true three way mergetool and run `git mergetool` when you you get conflicts. – CB Bailey Mar 10 '12 at 18:39
  • Good suggestion. I didn't consider this because I really don't like any of the merge tools and have found fixing my conflicts directly in an editor better. I found [this](http://stackoverflow.com/questions/1817370/using-ediff-as-git-mergetool) as a possible solution and I think customizing the [mergetool](http://schacon.github.com/git/git-mergetool.html) is definitely the way to go. The question is now, what to do with my question? Should I click "Answer Your Question" at the bottom of the page, or do nothing? – flooose Mar 11 '12 at 07:38
  • Question not directly answered. Clarification may help. Of the events git supports for hooking, including (based on default `.git/hook/*.sample` files): 1. `post-checkout` 2. `post-commit` 3. `post-merge` 4. `post-receive` 5. `post-update` 6. `pre-applypatch` 7. `pre-commit` 8. `pre-push` 9. `pre-rebase` ... does one or more apply during a merge conflict? As git forces `root` ownership and limited permissions during merge conflicts, ordinary users cannot resolve merge conflicts. A merge conflict hook would allow for setting usable ownership, permissions. – pd_au Jan 26 '20 at 03:33
  • I never run git as root and should have run into permission issues if what you say is true. – flooose Jan 27 '20 at 12:57

2 Answers2

2

As suggested by Charles Bailey, the best way to do this is by customizing the mergetool. Using this guide, I came up with this simple way to have merge conflicts opened in my editor:

[merge]
  tool = emacs
[mergetool "emacs"]
  cmd = $editor \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"

Since Charles Bailey never answered how I should give him credit for this, I hope this is an appropriate way to finally close this question.

flooose
  • 499
  • 8
  • 25
0

I think there maybe two ways, as mentioned you by floose you could edit your mergetool or perhaps you could create another alias using:

for i in $(git ls-files -u | cut -f 2 | sort -u); do $EDITOR $i; done
binarycreations
  • 3,091
  • 6
  • 32
  • 38