Created
November 24, 2025 12:45
-
-
Save andreasvirkus/14e70256033bb78b3936bd430eb7c91f to your computer and use it in GitHub Desktop.
Place in .git/hooks/pre-push
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Get the current branch name | |
| current_branch=$(git rev-parse --abbrev-ref HEAD) | |
| # Check if pushing to main branch | |
| if [[ "$current_branch" = "main" ]]; then | |
| echo "⚠️ You're about to push to the main branch!" | |
| echo -e "Current branch: $current_branch\n" | |
| # Redirect stdin from the terminal | |
| exec < /dev/tty | |
| read -p "Are you sure you want to continue? (y/N): " -n 1 -r | |
| echo "" | |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
| echo "Push cancelled." | |
| exit 1 | |
| fi | |
| fi | |
| # Allow the push to continue | |
| exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Get the current branch name | |
| current_branch=$(git rev-parse --abbrev-ref HEAD) | |
| # Protected branches | |
| protected_branches=("main" "master" "production") | |
| # Check if pushing to a protected branch | |
| for branch in "${protected_branches[@]}"; do | |
| if [[ "$current_branch" == "$branch" ]]; then | |
| echo "⚠️ You're about to push to the '$current_branch' branch!" | |
| # Redirect stdin from the terminal (required for hooks) | |
| exec < /dev/tty || exit 1 | |
| read -p "Are you sure you want to continue? (y/N): " -n 1 -r | |
| echo | |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
| echo "❌ Push cancelled." | |
| exit 1 | |
| fi | |
| echo "✅ Push approved." | |
| exit 0 | |
| fi | |
| done | |
| # Allow non-protected branches to push without prompt | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment