Git: How to Prevent a Repository Branch from Being Published


Sometimes, you need to keep a branch private, and to protect yourself from accidentally clicking ‘Publish Branch,’ it’s a good idea to prevent its publication. On Windows, you can do it like this…

In the folder .git/hooks, create a file named pre-push (without an extension) and place the following content inside:

#!/bin/sh

# List of branches to protect
PROTECTED_BRANCHES="local-branch-name"

# Get the name of the branch being pushed
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/,,')

# Check if the current branch is in the list of protected branches
if echo "$PROTECTED_BRANCHES" | grep -w "$current_branch" > /dev/null; then
    echo "Pushing to branch '$current_branch' is not allowed."
    exit 1
fi

This entry was posted in Git (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.