Skip to content

Instantly share code, notes, and snippets.

@refactorsaurusrex
Last active December 23, 2019 14:58
Show Gist options
  • Save refactorsaurusrex/2259e14c981e2b353df1f8b6066769dc to your computer and use it in GitHub Desktop.
Save refactorsaurusrex/2259e14c981e2b353df1f8b6066769dc to your computer and use it in GitHub Desktop.
git 'prepare commit msg' hook that rejects merge commits when the source branch is behind the target branch
#!/bin/sh
if [ "$2" = "merge" ]; then
githead=$(env | grep GITHEAD)
source="${githead##*=}"
target=$(git symbolic-ref HEAD)
behind=$(git rev-list --left-only --count $target...$source)
if [ $behind -gt 0 ]; then
echo "Aborting: The source branch is $behind commits behind the target branch."
exit 1
fi
fi
#ref https://superuser.com/a/926038/51794
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment