Skip to content

Instantly share code, notes, and snippets.

@melkorm
Last active July 2, 2020 11:19
Show Gist options
  • Save melkorm/ffd3a3e77801354e3972 to your computer and use it in GitHub Desktop.
Save melkorm/ffd3a3e77801354e3972 to your computer and use it in GitHub Desktop.
GIT: Pre receive hook to prevent pushing unresolved conflicts
#!/usr/bin/env bash
read old_sha new_sha refname

# if we delete branch don't do anything
if [[ "$new_sha" =~ ^0+$ ]]; then
   exit 0
fi

# if we push new branch we need to set correct old_sha
if [[ "$old_sha" =~ ^0+$ ]]; then
    old_sha=`git rev-list $new_sha --not --branches | tail -n 1`
fi

# check if code includes conflict markers
if git diff "$old_sha" "$new_sha" | grep -qE '^+?(<<<<<|>>>>>)'; then
    echo "Saw a conflict marker in $(basename "$refname")."
    exit 1
fi

Script based on: http://stackoverflow.com/questions/11358225/how-to-prevent-push-if-there-are-unresolved-conflicts-using-hook-in-git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment