Created
October 4, 2019 09:28
-
-
Save endymion1818/7255ee12bd014dbd14e1b0040ed99ea4 to your computer and use it in GitHub Desktop.
protected branches (git hook)
This file contains 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 | |
protected_branches=( production master ) | |
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') | |
for i in "${protected_branches[@]}" | |
do | |
protected_branch=$i | |
if [ $protected_branch = $current_branch ] | |
then | |
read -p "You're about to push a protected branch, is that what you intended? [y|n] " -n 1 -r < /dev/tty | |
echo | |
if echo $REPLY | grep -E '^[Yy]$' > /dev/null | |
then | |
exit 0 # push will execute | |
fi | |
exit 1 # push will not execute | |
else | |
exit 0 # push will execute | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
git config --global init.templatedir '~/.git-templates'
This tells git to copy everything in ~/.git-templates to your per-project .git/ directory when you run git init
Create a directory to hold the global hooks:
mkdir -p ~/.git-templates/hooks
Write your hooks in ~/.git-templates/hooks.
For example, here's a post-commit hook (located in ~/.git-templates/hooks/post-commit):
#!/bin/sh
Copy last commit hash to clipboard on commit
git log -1 --format=format:%h | pbcopy
Add other post-commit hooks
Make sure the hook is executable.
chmod a+x ~/.git-templates/hooks/post-commit
Re-initialize git in each existing repo you'd like to use this in:
git init
NOTE if you already have a hook defined in your local git repo, this will not overwrite it.