Skip to main content

Hooks

git hooks

pre-commit-msg

This example adds the branch name as prefix to the commit message:

  • create the file ~/.githooks/prepare-commit-msg
    #!/bin/sh
    #
    # Automatically adds branch name every commit message.
    #
    NAME=$(git branch | grep '*' | sed 's/* //')
    
    echo "[$NAME]"': '$(cat "$1") > "$1"
    
  • execute git config --global core.hooksPath ~/.githooks to use this hook at every repository

pre-push example

  • create the file ~/.githooks/pre-push

    while true; do
        read -r -p "Have you executed 'composer test' already? (y|n): " yn < /dev/tty
        case $yn in
            [Yy]*) exit 0 ;;
            [Nn]*)
            echo "\e[31mPlease execute the command first \e[39m";
            exit 1;
            ;;
            *) echo "Please answer [y]es or [n]o." ;;
        esac
    done
    
  • execute git config --global core.hooksPath ~/.githooks to use this hook at every repository