I have a Git pre-commit hook that prevents me from committing to master unless overridden, in order to encourage developing on branch.
However I would like to automatically allow merge commits to master. Is there a way to identify a merge commit from my pre-commit hook script? The script looks like this:
#!/bin/bash
BRANCH=`git branch --color=never| grep '^*'|cut -c3-`
if [ "${BRANCH}D" == "masterD" -a "${GIT_COMMIT_TO_MASTER}D" != "trueD" ]
then
echo "Commit directly to master is discouraged."
echo "If you want to do this, please set GIT_COMMIT_TO_MASTER=true and then commit."
exit 1
fi
SOLVED: For anyone looking for a cut-and-paste, the working version of this hook script is:
#!/bin/bash
BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "${BRANCH}" == "master" -a "${GIT_COMMIT_TO_MASTER}" != "true" ]
then
if [ -e "${GIT_DIR}/MERGE_MODE" ]
then
echo "Merge to master is allowed."
exit 0
else
echo "Commit directly to master is discouraged."
echo "If you want to do this, please set GIT_COMMIT_TO_MASTER=true and then commit."
exit 1
fi
fi