Skip to content

Instantly share code, notes, and snippets.

@endymion1818
Created October 4, 2019 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endymion1818/7255ee12bd014dbd14e1b0040ed99ea4 to your computer and use it in GitHub Desktop.
Save endymion1818/7255ee12bd014dbd14e1b0040ed99ea4 to your computer and use it in GitHub Desktop.
protected branches (git hook)
#!/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
@endymion1818
Copy link
Author

endymion1818 commented Nov 4, 2020

  1. Enable git templates:
    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

  1. Create a directory to hold the global hooks:
    mkdir -p ~/.git-templates/hooks

  2. 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

  1. Make sure the hook is executable.
    chmod a+x ~/.git-templates/hooks/post-commit

  2. 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.

@endymion1818
Copy link
Author

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