Skip to content

Instantly share code, notes, and snippets.

@yanker
Last active May 20, 2024 12:09
Show Gist options
  • Save yanker/1606794062d2579d88defa2b556da9a5 to your computer and use it in GitHub Desktop.
Save yanker/1606794062d2579d88defa2b556da9a5 to your computer and use it in GitHub Desktop.
Protected branches Push

Hook pre-push - Protected Branches Push

#!/bin/sh

# Rename file in .git/hooks/pre-push 

RED='\033[0;31m'
NC='\033[0m' # No Color

#protected_branches=(master develop)
protected_branches=(main)
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
for branch in "${protected_branches[@]}"
do
    if [ $branch = $current_branch ]
    then
        printf "${RED}[Policy] The push to the '${current_branch}' branch is forbidden!.${NC}\n"
        exit 1
    fi
done

exit 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment