Applying a git post-commit hook to all current and future repositories
Asked Answered
U

4

332

I've written a Git post-commit hook and it works correctly. However, I want to add this hook to apply to all current (and future) Git repositories I am working on. I tried adding the hook to my ~/.git/hooks/ directory instead of in the hooks directory in the project directory, however, this did not seem to work.

Is there a way to create global Git hooks that will apply to all repositories on my system (without having to copy them into each project directory)? If not, what would be the best solution going forward -- perhaps a git-init template?

Ujiji answered 19/2, 2010 at 1:24 Comment(3)
As of Git 2.9 this best approach to this has changed: VonC's answer is a better bet. https://mcmap.net/q/20392/-applying-a-git-post-commit-hook-to-all-current-and-future-repositoriesRuddy
Does this answer your question? change default git hooksStylet
"project directory" is (probably) a GitLab'ism (for the repository root).Huzzah
F
296

I want to add this hook to apply to all current (and future) git repositories I am working on

With git 2.9+ (June 2016), all you would do is:

git config --global core.hooksPath /path/to/my/centralized/hooks

See "change default git hooks": this has been done to manage centralized hooks.


But, as noted by Adam Lindberg in the comments:

setting a global hooks path disables all local hooks in you repos!

Depending on your use case, that can be the goal, if you want to, as the OP puts it, "create global Git hooks that will apply to all repositories on my system (without having to copy them into each project directory)".

But if you are unaware of that side effect, and still want to retain some local hooks... those would be ignored when core.hooksPath is set.

Feminism answered 18/5, 2016 at 7:42 Comment(7)
if i have an exisiting repo and want all other dev's who pull changes to have an updated pre-commit hook for example how would i do this ? thank youIgenia
@Igenia do you mean https://mcmap.net/q/20559/-update-pre_commit-hook-file-on-version-control was not completely clear?Feminism
@Igenia You would need to setup a shared folder accessible by all devs, for them to reference in their own local config.Feminism
unfortunately i still don't quite get it. within the repo there is /.git/hooks/pre_commit can i point it to that ?Igenia
@Igenia I have answered you on your original question.Feminism
Note that setting a global hooks path disables all local hooks in you repos!Hydraulic
@AdamLindberg Good point. I have edited the answer to make that clearer.Feminism
P
322

As of Git 1.7.1, you can set init.templatedir in your gitconfig to tell Git where to look for templates.

Set it like this:

git config --global init.templatedir '~/.git_template'

Afterward, new repositories you create or clone will use this directory for templates. Place the hooks you want in ~/.git_template/hooks. Existing repositories can be reinitialized with the proper templates by running git init in the same directory .git is in.

For Git versions older than 1.7.1, running git init --template ~/.git_template will work if you're like me and still want to manage your .git_template directory along with the rest of your dot files. You can also use the $GIT_TEMPLATE_DIR environment to tell git init where your template directory is.

Parathyroid answered 12/1, 2012 at 21:34 Comment(13)
Great answer. If anybody else wonders if re-running git init on an existing repo wipes it - it does not, see: stackoverflow.com/questions/5149694/…Lyonnesse
For me it didn't work with the relative path to the git template folder for copying files using git init on existing repositories. I had use the full path instead.Achromatize
Just an fyi, ~/.git_template is not automatically created for you with the command. You still have to do that yourself with mkdir ~/.git_templateRoentgen
On windows without quotation: ` git config --global init.templatedir d:\git\.git_template\ `Billfold
Tip: The template hooks are copied. If you want to be able to update the global hooks together, put the hook elsewhere and add a symbolic link in the template directory (on Linux). Make sure the link path is absolute.Angelicangelica
There is now a way to have centralized hooks, see @VonC 's answerButyrin
Running git init didn't actually update the hooks for me. It seems it will not overwrite files that are already there. stackoverflow.com/questions/10791486/. Instead you need to remove the old hook files first.Antifederalist
@Parathyroid what about cloning an app? will it work for that or if I want to make it, what should be done? Thanks!Namecalling
As its a global config, how can I revert? will delete ~/.git_template work?Namecalling
isn't --global user-based? I need this to work with multiple users (e.x. Jenkins build on commit). How to setup with --system? Just modify system config file to add init.templatedir='~/.git_template' or it only works with global config?Allman
So to answer my own question (after some tests), yes - it is possible with --system config. Just add sudo in front as it is accessing system folders. If You can't use sudo, then edit or create etc/gitconfig file. For creating: 1st line [init], 2nd linetemplatedir = ~/.git_templateAllman
In Windows CMD, you should remove single quotes! E.g. git config --global init.templatedir ~/.git-template. Also, the default template folder is in: C:\Program Files\Git\mingw64\share\git-core\templatesBoastful
the link is brokenDarryldarryn
F
296

I want to add this hook to apply to all current (and future) git repositories I am working on

With git 2.9+ (June 2016), all you would do is:

git config --global core.hooksPath /path/to/my/centralized/hooks

See "change default git hooks": this has been done to manage centralized hooks.


But, as noted by Adam Lindberg in the comments:

setting a global hooks path disables all local hooks in you repos!

Depending on your use case, that can be the goal, if you want to, as the OP puts it, "create global Git hooks that will apply to all repositories on my system (without having to copy them into each project directory)".

But if you are unaware of that side effect, and still want to retain some local hooks... those would be ignored when core.hooksPath is set.

Feminism answered 18/5, 2016 at 7:42 Comment(7)
if i have an exisiting repo and want all other dev's who pull changes to have an updated pre-commit hook for example how would i do this ? thank youIgenia
@Igenia do you mean https://mcmap.net/q/20559/-update-pre_commit-hook-file-on-version-control was not completely clear?Feminism
@Igenia You would need to setup a shared folder accessible by all devs, for them to reference in their own local config.Feminism
unfortunately i still don't quite get it. within the repo there is /.git/hooks/pre_commit can i point it to that ?Igenia
@Igenia I have answered you on your original question.Feminism
Note that setting a global hooks path disables all local hooks in you repos!Hydraulic
@AdamLindberg Good point. I have edited the answer to make that clearer.Feminism
J
73

If you want them everywhere on your system (including users besides you), you can modify the contents of the installed template directory - those are in $PREFIX/share/git-core/templates/hooks, where $PREFIX is probably /usr/local or /usr.

If you want this to just be for you, then yes, the simplest thing would be the --template option of git-init. You could easily keep a personal template directory which has symlinks back to the installed version of defaults you want to keep (individual hooks, the info directory...) and then your own content in hooks/post-commit and anything else you want to customize.

Janinejanis answered 19/2, 2010 at 1:49 Comment(6)
Thanks, this worked out well. And to retroactively apply it to my existing projects, I just ran git init again and it added my new hook.Ujiji
This is a neat workaround, but it would require that you change all your repos. This is feasable, but isn't there some possibility with a plugin or something (this is how it is done at Bazaar)?Slotnick
For SourceTree's embedded git on OS X, they're in /Applications/SourceTree.app/Contents/Resources/git_local/share/git-core/templates/hooksIrritative
For Windows this is found in the git install directory under mingw64\share\templates\hooks (or mingw32 for 32-bit)Masoretic
@Masoretic it's now at mingw64\share\git-core\templates\hooks (gfw 2.25)Mindful
Where the answer talks about the contents of the installed template directory - those are in $PREFIX/share/git-core/templates/hooks - would that apply to an on-prem GitHub instance, so that ALL repos in our org get the hooks?Gerent
W
4

A minimalist approach is to create a git_hooks/ directory in your repository to track the hooks that you write for that project, bring it to the attention of future users by mentioning it in a README, and rely on them to do the right thing after they have cloned. I have cogitated on this for a while and chose an incremental approach. Down the road I might consider using a tool like git-hooks.

Whipperin answered 1/6, 2012 at 20:50 Comment(2)
This is so, so far away from being a real solution that I had to downvote, sorry.Yellowlegs
the link is brokenDarryldarryn

© 2022 - 2024 — McMap. All rights reserved.