Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active August 20, 2018 11:22
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 YumaInaura/8b482ee687832f7515f2a48db1c55130 to your computer and use it in GitHub Desktop.
Save YumaInaura/8b482ee687832f7515f2a48db1c55130 to your computer and use it in GitHub Desktop.
Git — Why "global" githooks does not work? ( Actually .gitconfig "init" directive works only when git init or clone )

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.

Points

  • Anytime current repository git hooks is used. ( Template file not used directly )
  • Hooks or other templates will be copies when git clone or git init.
  • If current local repository already has template file ( e.g rm .git/hooks/pre-push ) then you need once remove it and re init git init.

~/.gitconfig init section

[init]
  templatedir = ~/.git_templates

You can specify template directory in your .gitconfig.

Consider why "init" section named "init". ( not like "global" )

Example. ~/.git_templates/hooks/pre-push

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.

Git clone

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

Remove hooks and re init

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

Example. Check working of pre-push

git push

You can not git push anyway
error: failed to push some refs to 'https://github.com/YumaInaura/playground'

Versions

  • git version 2.17.1

Ref

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.

Git - git-init Documentation

Links

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