Manually editing merge conflicts can lead to a lot of pain. What if you missed a conflict? Now you have >>>>>>>>>>> and <<<<<<<<<<<<<< all around your code base.

Good news, it’s really easy to deny those changes by utilizing git-hooks.

On every commit Git runs a command (or hook). In the case of committing, Git runs the pre-commit hook.

By scanning for the merge conflict markers in our pre-commit hook we can deny a commit and keep those less-than-useful markers out of the repository. Of course, if you really need to commit something, you can do that as well but it takes a little extra work. Think of this hook as a safety net, it won’t gaurantee safety but it does help a lot.

Setting up the pre-commit hook

  • Copy the pre-commit hook file into your-working-copy/.git/hooks/pre-commit
  • Make the script executable (chmod u+x pre-commit) {% gist jonDowdle/22ed6493fdcbfa3c985a %}

Overriding the hook

By default git commit will run the pre-commit hook and deny changes if it doesn’t return successfully. If you need to circumvent this, you only need to pass in a flag git commit --no-verify.

I can’t speak for all of the Git front ends, but if you use SourceTree, you can override the hook by selecting the following option:

Bypass Git hooks in SourceTree