11

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.

J0e3gan
  • 8,740
  • 10
  • 53
  • 80
  • 1
    Did you find an explanation for this? I just ran into this issue. .git/hooks was missing when cloning a repository from a Bitbucket server. I wonder if it is a SourceTree defect. – Jason Harrison Aug 01 '17 at 19:10

3 Answers3

9

The defined hooks in a git repository are always local to this repository. They are no part of the git history and are not transferred when cloning, fetching or pushing.

Thus, just by cloning an existing repo with hooks, the clones repo will have none of the upstream hooks. You would have to add them manually to the cloned repository.

This question explains some options to help you achieve this.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
  • 2
    Thanks for the explanation; and the question you referenced explains further very well too. What I still wonder then is where the `.git/hooks` directories in some of the clones came from, why `.git/hooks`' presence/absence would not be uniform across the clones if I have followed the same process to create and start using each clone. – J0e3gan Nov 13 '15 at 05:31
2

I'm posting this a few years later.

This does seem like a SourceTree thing (as theorized by Jason above). More specifically if one uses the Embedded Git from SourceTree. If I use System Git (I'm on a Mac), then the hooks directory is created and populated with samples. Cloning via command-line yields the same result. When using the Embedded Git, then there is no hooks directory.

Screenshot from the git options in Preferences where you can select between the two.

enter image description here

Mobile Ben
  • 7,121
  • 1
  • 27
  • 43
1

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

Abhijeet Khangarot
  • 1,281
  • 1
  • 16
  • 25