Useful snippets

    git hooks

    2026-02-25

    To activate pre-push, rename .git/hooks/pre-push.sample to .git/hooks/pre-push


    Example script:

    branch_name=$(git symbolic-ref HEAD 2>/dev/null)
    

    echo "$branch_name"

    if [ "$branch_name" != "refs/heads/min-branch" ]; then echo "You can't push this branch." # also prevents git push --force-with-lease exit 1 fi

    exit 0

    should work given that this command:

    git symbolic-ref HEAD 2>/dev/null

    gives: refs/heads/min-branch

    and:

    git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3

    gives min-branch

    Bash. check if a file exists

    2026-03-03

    Check if prepush is activated or not

    lsprepush() {
      if [[ -f ".git/hooks/pre-push" ]]; then
        echo ".git/hooks/pre-push was found:"
        echo "pre-push is activated"
      else
        if [[ -f ".git/hooks/pre-push.sample" ]]; then
          echo ".git/hooks/pre-push was not found:"
          echo "but .git/hooks/pre-push.sample was found:"
          echo "pre-push is deactivated"
        else
          echo "Hmmmmm"
        fi
      fi
    }