Sub topic 6
Sharing hooks with the team using core.hooksPath
This chapter assumes you can write a basic pre-commit hook and know that a hook is an executable file that Git runs at a fixed point in the workflow. You should also be comfortable with git add, git commit and git push.
Why .git/hooks does not travel
Git looks for hooks in .git/hooks by default. That directory lives inside the repository's metadata, and Git does not version it. When a teammate clones the repository, they get your source files but none of your hooks.
This means a hook you wrote yesterday protects only your own clone. Everyone else commits without it.
The fix: a tracked hooks directory
Put the hooks in an ordinary directory inside the working tree, such as .githooks. Git versions it like any other directory. Then tell Git to look there with the core.hooksPath setting.
mkdir -p .githooks
chmod +x .githooks/pre-commit
git config core.hooksPath .githooks
git add .githooks
git commit -m "Add shared pre-commit hook"Here is what each step does:
mkdir -p .githookscreates the directory.chmod +xsets the executable bit. Git skips a hook that is not executable.git config core.hooksPath .githooksmakes Git look in.githooksinstead of.git/hooksfor this clone.git addandgit commitstore the hook files in the repository history.
The file mode is recorded by Git, so the executable bit is kept for teammates on Linux and macOS when they clone.
The one step Git will not do for you
The core.hooksPath setting is stored in .git/config. Like .git/hooks, that file is not versioned. A clone has the hook files, but Git does not use them until each person runs the git config command once.
This is a deliberate safety rule. If cloning a repository could switch on arbitrary scripts automatically, a malicious repository could run code on your machine the moment you committed. So activation must be an explicit action by the person who owns the clone.
Making setup easy to remember
Teams usually reduce the manual step to something hard to forget. Common options:
- Document the command in the README under a "Getting started" heading.
- Add a setup script or a
make setuptarget that runsgit config core.hooksPath .githooksalong with other first-time steps. - Run the command from a package manager's install step, so it happens when dependencies are installed.
A setup script can also check whether the path is set, and print a warning if it is not.
Relative paths
A relative value such as .githooks is resolved from the directory where the hook runs, which is the top of the working tree. That keeps the setting identical for every teammate, whatever their home directory is. An absolute path would break on other machines, so prefer the relative form for shared hooks.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The hook never runs | `core.hooksPath` is not set in this clone | Run `git config core.hooksPath .githooks` |
| The hook never runs, and the path is set | The file is missing the executable bit | Run `chmod +x .githooks/pre-commit` |
| The hook works for you but not a teammate | They cloned but did not run the config command | Add the command to your setup steps |
| Old hooks in `.git/hooks` stop running | `core.hooksPath` replaces the default directory | Move or copy them into `.githooks` |
You can check which path is active with git config core.hooksPath. If it prints nothing, Git is using .git/hooks.
What sharing does not give you
Shared hooks are still voluntary. A teammate who skips the setup, or who commits with --no-verify, bypasses every check. Sharing hooks makes the right behavior easy, but it does not enforce it. A later chapter covers why a CI check is still required as a backstop.
Exercise
A teammate clones your repository, which contains an executable .githooks/pre-commit file. They commit a change with a trailing whitespace error, and the commit succeeds with no hook output. Give the most likely cause and the command that fixes it.
Show answer
The teammate never ran git config core.hooksPath .githooks in their clone. The setting lives in .git/config, which is not cloned, so Git still looks in .git/hooks and finds no hook. Running git config core.hooksPath .githooks once in that clone fixes it.
Exercise
Why does Git require each person to opt in to core.hooksPath instead of reading the setting from a file in the repository?
Show answer
If a repository could activate its own scripts on clone, anyone who published a repository could run arbitrary code on the machines of people who committed to it. Requiring an explicit local command keeps that decision with the person who owns the clone.