118

If I create a new hook script in my local repository in repo/.git/hooks/post-commit and then I run "git push" are the hooks pushed to the remote? Then, when the other developers run "git pull" from the same origin will they get my new hooks?

slacy
  • 11,397
  • 8
  • 56
  • 61
  • 3
    Perhaps this help http://stackoverflow.com/questions/3462955/putting-git-hooks-into-repository – René Höhle Aug 31 '12 at 20:41
  • Related (with very useful answers): [Can Git hook scripts be managed along with the repository?](https://stackoverflow.com/q/427207/3258851) – Marc.2377 Jan 20 '20 at 01:05

3 Answers3

101

No. Hooks are per-repository and are never pushed. Similarly, the repo config isn't pushed either, nor is anything in .git/info, or a number of other things.

Pushing and pulling only exchanges branches/tags and commit objects (and anything reachable from a commit, e.g. trees, blobs).

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • 21
    Is there a way to have hooks in central and get them pulled on each clone, and applied. – f1wade Dec 13 '16 at 15:06
  • 9
    @f1wade You could add a `.githooks` directory which would be version controlled then either sym-link or create wrapper hook scripts from inside `.git`. Cloning wouldn't automatically wire in `.githooks` but once wired in you can push and pull new versions of the hook scripts. – Philip Couling Jan 11 '19 at 12:34
54

No, git hooks are not pushed or pulled, as they are not part of the repository code.

Please refer to the documentation for a list of simple client-side and server-side hooks.

If you want to enable some hooks for all clients that clone or pull from a given repository, you have to add the hooks to your codebase and then create your own script to copy them into, or link to them from repo/.git/hooks/.

Marco Leogrande
  • 8,050
  • 4
  • 30
  • 48
  • 4
    but how does the copy script get called? – Adam Johns Dec 18 '15 at 19:20
  • 8
    @AdamJohns It gets called by the user/developer, probably in the form of a `setup.sh` that the dev runs when they first clone the repository. After this, updates to the hooks can happen automatically by having a hook that re-runs an script updating them after every pull, for example. So you don't have to rely on the developer to do it themselves. – cm92 May 20 '16 at 16:23
40

Sadly no but since git 2.9 you can place them into .githooks folder (as others mentioned) and run:

git config --local core.hooksPath .githooks/

So no need to create symlinks or copy files.

Arad Alvand
  • 8,607
  • 10
  • 51
  • 71
Marco Medrano
  • 2,530
  • 1
  • 21
  • 35