Sub topic 6
Sharing hooks with the team using core.hooksPath
This chapter assumes you can write an executable hook script, you know that a non-zero exit status stops the commit, and you understand the difference between the staging area and the working tree. It covers why hooks are not shared by default, how core.hooksPath fixes that, and what can still go wrong.
Why hooks are not shared by default
Git looks for hooks in .git/hooks inside your repository. That directory is part of the repository's metadata, not part of the project's tracked files. Three consequences follow.
git addcannot track anything inside.git, so you cannot commit a hook there.git clonedoes not copy the hooks of the source repository. A new clone gets only the inactive*.samplefiles from Git's templates.- Two people on the same project can run different checks, or none at all, without noticing.
A check that only some contributors run is a weak check. The fix is to keep hook scripts in a normal tracked directory and tell Git to look there.
How core.hooksPath works
core.hooksPath is a Git configuration variable. When it is set, Git looks for hooks in that directory instead of .git/hooks. A relative path is resolved from the directory where the hook runs, which for most hooks is the top level of the working tree. That makes a value such as .githooks portable across machines.
The directory can be any name. .githooks is a common choice because it is visible in the repository and clearly says what it holds.
mkdir -p .githooks
chmod +x .githooks/pre-commit
git config core.hooksPath .githooks
git add .githooks
git commit -m "Add shared pre-commit hook"Step by step:
mkdir -p .githookscreates the tracked directory.chmod +xsets the executable bit. Git ignores a hook file that is not executable.git config core.hooksPath .githookswrites the setting to.git/configof your local clone.git addandgit commitrecord the hook script in history, so teammates receive it with the next pull.
Notice that step 3 is local. The configuration lives in .git/config, which is not versioned. Committing the scripts shares the code, but every person still has to run the git config command once per clone.
A shared hook to start with
Put a real check in the directory so the setup is useful. This hook rejects staged changes that contain whitespace errors or leftover conflict markers.
#!/bin/sh
# .githooks/pre-commit
# Reject the commit if the staged changes contain whitespace errors or conflict markers.
if ! git diff --cached --check; then
echo "pre-commit: fix the problems above or commit with --no-verify." >&2
exit 1
fi
exit 0git diff --cached --check inspects the staged changes and exits non-zero when it finds a problem. The hook passes that failure on as exit status 1, and Git aborts the commit.
A second shared hook can inspect the staged version of each shell script. Reading the file with git show ":$f" returns the staged content, not the copy in your working tree.
#!/bin/sh
# .githooks/pre-commit
# Run shellcheck on the staged version of each added, copied or modified .sh file.
status=0
for f in $(git diff --cached --name-only --diff-filter=ACM -- '*.sh'); do
git show ":$f" | shellcheck - || status=1
done
exit $statusThe loop splits on whitespace, so it does not handle file names that contain spaces. For a team repository, either forbid such names or switch to a null-delimited approach.
Both examples are alternative bodies for the same file, .githooks/pre-commit. A hook directory holds one file per hook name, so to run both checks you combine them into one script or have the file call other scripts.
Getting everyone to enable it
Because the setting is local, a new contributor who clones the repository has no hooks active. Teams usually pick one of these approaches.
| Approach | How it works | Trade-off |
|---|---|---|
| Documented command | The README tells people to run `git config core.hooksPath .githooks` | Simple, but relies on people reading and remembering |
| Setup script | A script such as `scripts/setup.sh` runs the config command along with other setup | One command, still manual |
| Build tool integration | A `make setup`, package install step or similar runs the config command | Automatic for people who use the build tool |
| Hook manager | A tool installs and updates hooks for you | More moving parts, covered in a later chapter |
A setup script can be as small as this.
#!/bin/sh
# scripts/setup.sh
# Point Git at the shared hooks directory for this clone.
set -e
cd "$(git rev-parse --show-toplevel)"
git config core.hooksPath .githooks
echo "Hooks enabled from .githooks"Whichever you choose, treat it as a convenience and not a guarantee. Someone can skip the step, and the later chapter on bypassing hooks explains why CI must repeat the important checks.
Scope of the setting
git config core.hooksPath .githooks without extra flags writes to the repository's local configuration. You can also set it at other levels.
--local(the default when run inside a repository) affects only this clone.--globalaffects every repository for your user. Use it with care: a global path replaces.git/hookseverywhere, so repositories that rely on their own local hooks stop running them.- A project cannot force a setting onto your machine. Git deliberately does not read configuration from tracked files, because that would let a cloned repository run arbitrary code on your computer.
That last point explains the design. Hooks are code that runs with your permissions. Requiring each person to opt in by running a command is a security boundary, not an oversight.
Pitfalls and edge cases
Setting the path replaces the default directory
When core.hooksPath is set, Git does not also look in .git/hooks. A hook that previously lived there silently stops running. If you move to a shared directory, copy over any local hooks you still need.
A missing executable bit
A hook file without the executable bit is skipped. Git prints a hint in recent versions, but older ones say nothing. Git records the executable bit of tracked files in the file's mode, so committing the file with chmod +x already applied carries the bit to teammates on Unix-like systems. To fix an existing file that was committed without it, run git update-index --chmod=+x .githooks/pre-commit. This matters on Windows, where the file system does not store the bit.
Relative paths and where hooks run
Git resolves a relative core.hooksPath from the directory in which it runs the hook. For most client-side hooks, that is the top level of the working tree. Some commands behave differently in bare repositories or linked worktrees. If you see a hook not found in a worktree, set an absolute path or check with git rev-parse --git-path hooks, which prints the hooks directory Git is actually using.
git rev-parse --git-path hooks
git config --show-origin --get core.hooksPathThe second command shows the value and the configuration file it comes from. That answers the question of which level, local or global, is in effect.
Line endings on Windows
A hook script saved with CRLF line endings fails with an error such as /bin/sh^M: bad interpreter. Prevent it by marking the directory in .gitattributes:
.githooks/* text eol=lfDifferent tools on different machines
A shared hook runs on every teammate's machine. If it calls shellcheck and someone has not installed it, the hook fails with "command not found". Decide whether a missing tool should block the commit or only print a warning, and write that in the script. Blocking on a missing tool makes onboarding harder. Skipping silently hides the problem. A clear message that names the tool and how to install it is usually the best choice.
#!/bin/sh
# .githooks/pre-commit
if ! command -v shellcheck >/dev/null 2>&1; then
echo "pre-commit: shellcheck is not installed; skipping shell checks." >&2
exit 0
fiUpdating hooks
Because the scripts are tracked, a git pull that changes .githooks updates everyone's hook the next time they pull. There is no separate install step for updates. This also means a broken hook merged to the main branch blocks every contributor's next commit, so test hook changes before you merge them.
Comparing the options
| Option | Shared through Git | One-time setup per clone | Notes |
|---|---|---|---|
| Copy files into `.git/hooks` | No | Yes, manual copy | Easy to forget, easy to drift |
| Symlinks from `.git/hooks` to a tracked directory | Partly | Yes, create links | Fragile on Windows |
| `core.hooksPath` to a tracked directory | Yes | One config command | Needs Git 2.9 or later |
| Hook manager | Yes | One install command | Adds a dependency |
core.hooksPath was added in Git 2.9. Check your version with git --version if the setting seems to have no effect.
Exercise
A teammate pulls your commit that adds .githooks/pre-commit with the executable bit set. Their next commit is not checked. Give two possible reasons and one command that helps you diagnose the first.
Show answer
First, core.hooksPath is local configuration, so their clone still uses .git/hooks until they run git config core.hooksPath .githooks. Second, the executable bit may be missing on their system (for example on Windows or after a copy that dropped the mode), or their Git is older than 2.9. To check which hooks directory Git uses, run git rev-parse --git-path hooks, or run git config --show-origin --get core.hooksPath to see whether the setting exists and where it comes from.
Exercise
You set git config --global core.hooksPath ~/my-hooks to get a personal hook everywhere. A project you work on has its own script in .git/hooks/pre-commit that used to run. What happens to it, and how would you restore it for that project?
Show answer
It stops running, because a configured core.hooksPath replaces .git/hooks rather than adding to it. To restore it for that project, set a local value that overrides the global one, for example git config core.hooksPath .git/hooks inside that repository, or remove the global setting with git config --global --unset core.hooksPath.