Here is how you can have some default git hooks auto installed with every new repo you clone. I use this to install
a pre-push
hook that adds a confirmation step to any push to main so that I don't accidentally push up any changes.
- set up git to use a templatedir
git config --global init.templatedir '~/.git_template'
mkdir -p ~/.git_template/hooks
- add in the
pre-push
hook code in.git_template/hooks/pre-push
#!/bin/bash
protected_branch='main'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
if [ $protected_branch = $current_branch ]
then
read -p "You're about to push main, 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
-
make sure the new hook is marked as executable by running
chmod +x pre-push
on command line -
profit.
Stole the git template setup from the vim king, Tim Pope. https://tbaggery.com/2011/08/08/effortless-ctags-with-git.html
Stole the pre-push
hook from Ghost CTO, Hannah Wolfe. http://dev.ghost.org/prevent-master-push/