Article Image

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)

pre-commit script to prevent merge markers from being committed.

Author: Jon Dowdle <>

This simply searches the files that you are about to commit.

changed=$(git diff --cached --name-only)

if [[ -z "$changed" ]] then exit 0 fi

echo $changed | xargs egrep '[><]{7}' -H -I --line-number

If the egrep command has any hits - echo a warning and exit with non-zero status.

if [ $? == 0 ] then echo "\n\nWARNING: You have merge markers in the above files, lines. Fix them before committing.\n\n" exit 1

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

Blog Logo

Jon Dowdle

Jon Dowdle currently works on the internet at a lovely place called InVision.



Jon Dowdle's Blog

The personal blog of Jon Dowdle.

Back to Overview