I have several (Git for Windows 2.6.2 64-bit) repos that I created and started using the same way:
git init --bare <repoName>.git
in Git Bash.In trying to add a common post-commit
hook to these cloned repos, I noticed that only some of them had a .git/hooks
directory. Why?
I dug for questions/answers regarding missing .git/hook
directories and did not find anything on SO or more broadly. Git documentation mentions that repo initialization ensures the (.git/hooks/
) .sample
files are executable by default; but I really could not find anything that seemed to explain why my cloned repos sometimes contain .git/hooks
and sometimes do not.
Can anyone shed some light on the cause and effect at play here? Thanks.
Hooks are local to any given Git repository, and they are not copied over to the new repository when you run git clone . And, since hooks are local, they can be altered by anybody with access to the repository. This has an important impact when configuring hooks for a team of developers.
Git hooks are shell scripts found in the hidden . git/hooks directory of a Git repository. These scripts trigger actions in response to specific events, so they can help you automate your development lifecycle. Although you may never have noticed them, every Git repository includes 12 sample scripts.
Implementing Git Hooks Upon initializing a new project, Git populates the hooks folder with template files. To enable the hook scripts, simply remove the . sample extension from the file name. Git will automatically execute the scripts based on the naming.
The hooks are all stored in the hooks subdirectory of the Git directory. In most projects, that's . git/hooks .
As git hooks are not transferred while cloning, etc. thus a lot of times the hooks are defined in a separate directory (outside of .git directory which is not cloned or transferred).
This is mostly the case with team projects and open source repos. Thus they may have some scripts (for example in their Makefile
) which is removing the local .git/hooks
and adding some other directory containing the hooks (for example .githooks
in root of repo).
This is possible because you can easily configure git to set the core.hooksPath
configuration variable to your managed hooks directory for example:
git config core.hooksPath .githooks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With