Git — Why "global" githooks does not work? ( Actually .gitconfig "init" section works only when git init or clone )
I've thought I can set "global" githooks but probably it’s not.
Ah no wonder after understanding.
- Anytime current repository git hooks is used. ( Template file not used directly )
- Hooks or other templates will be copies when
git clone
orgit init
. - If current local repository already has template file ( e.g
rm .git/hooks/pre-push
) then you need once remove it and re initgit init
.
[init]
templatedir = ~/.git_templates
You can specify template directory in your .gitconfig.
Consider why "init" section named "init". ( not like "global" )
Put executable script as .git/hooks/pre-push
template.
mkdir -p ~/.git_templates/hooks/
echo "echo You can not git push anyway && exit 1" > ~/.git_templates/hooks/pre-push
chmod +x ~/.git_templates/hooks/pre-push
You need non zero exit code for prevent push.
When after git clone .git/hooks/pre-push
sciript will be copied from template file.
git clone https://github.com/YumaInaura/playground
cd playground # move to local repository
Check current repository's .git
directory.
cat .git/hooks/pre-push
echo You can not git push anyway can git push! && exit 1
If your local repository already has .git/hooks/pre-push
file then you need remove file and re init.
rm -f .git/hooks/pre-push
git init
git push
You can not git push anyway
error: failed to push some refs to 'https://github.com/YumaInaura/playground'
- git version 2.17.1
git-init - Create an empty Git repository or reinitialize an existing one
TEMPLATE DIRECTORY Files and directories in the template directory whose name do not start with a dot will be copied to the $GIT_DIR after it is created.