Git hook – checking branch name

Have you ever faced situation where you made wrong commit message and missed that and been embarrassed because some typos ?

Thankfully there are ways to avoid this. Called Git hooks.

In order to check commit message you can put script – content below into


NC='\033[0m' # No Color

get_initials () { array=( $(id -F) ); echo ${array[1]:0:1}${array[0]:0:1}; };

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

if ! grep -E "$INITIALS - $BRANCH - " $1; then
    echo "Your commit message should contain initials - branch name - description. Like below"
    printf "${RED}KP - ${BRANCH} - added new fixtures, changes on view${NC}\n"
    exit 1

So if we try to execute following command:

git commit -m "AD - 32 - test of git hook"

We will see

output when on wrong branch / wrong initials

Because my initials are KP and branch i should use is XX.
So when I execute proper command:

git commit -m "KP - XX - added new fixtures, changes on view"

I should see:

KP - XX - added new fixtures, changes on view
[XX 8f80226] KP - XX - added new fixtures, changes on view
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 xx