I made a pre-commit hook that forbids committing directly to master or devel branches on Git. This is something that you don’t want to do if you find yourself working under some Git model such as GitFlow or GitHubFlow. Specially if you are using GitFlow. Specially if you are not alone in your project.

Yes, I know that you can fix this after you make the commit by creating a new branch and then moving back the branch pointer for devel or master a few commits back, but I would waste less time if Git actually forbid me to make the commit in first place.

#!/bin/bash

FORBIDDEN_BRANCHES="master devel"
BRANCH=$(git rev-parse --abbrev-ref HEAD)

BOLD=$(tput bold)
YELLOW=$(tput setaf 3)
GRAY=$(tput setaf 8)
RESET=$(tput sgr0)

for branch in $FORBIDDEN_BRANCHES; do
 if [ "$branch" == "$BRANCH"  ]; then
  echo
  echo "${BOLD}You are currently in branch ${YELLOW}$BRANCH${RESET}."
  echo "    Committing from this branch would be a bad idea."
  echo "    Would you mind... changing to a different branch first?"
  echo
  echo "${GRAY}(Or you know, using the -n flag to bypass this message."
  echo " Hey, don't look at me! I'm a script, not a police officer!)"
  echo $RESET
  exit 1
 fi
done

You can also download the hook as a shell script. I don’t mind. It wouldn’t be that long if I didn’t declare all these variables but, hey, colors!

The hook in action
The hook in action. Note that I can still force the commit if I need or want to by using `git commit -n` (read the docs).