Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is .git/hooks sometimes missing in cloned repos?

I have several (Git for Windows 2.6.2 64-bit) repos that I created and started using the same way:

  1. Run git init --bare <repoName>.git in Git Bash.
  2. Clone the new repo using SourceTree.
  3. Add and commit initial files (through SourceTree or EGit).
  4. Blah, blah, blah....

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.

like image 722
J0e3gan Avatar asked Nov 12 '15 20:11

J0e3gan


People also ask

Do Git hooks get cloned?

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.

Are Git hooks stored in repo?

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.

How do I enable Git hooks?

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.

Where are Git hooks stored?

The hooks are all stored in the hooks subdirectory of the Git directory. In most projects, that's . git/hooks .


1 Answers

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

like image 138
Abhijeet Khangarot Avatar answered Nov 29 '22 00:11

Abhijeet Khangarot