Sharing Hooks with a Team
Sharing hooks means giving every member of a team the same Git hooks, automatically. It needs a deliberate setup, because Git never copies hooks: .git/hooks/ isn't part of any commit, so it isn't pushed, pulled or cloned. Otherwise, a repository could run any program on your computer as soon as you cloned it, which would be a serious security risk. The common solution is to commit the hooks in a normal folder and point Git at it with the core.hooksPath setting, or to use a hook manager that does this for you.
It is like a kitchen's hygiene checklist. Each cook could write their own on a sticky note, but they would all differ, and the new cook would have none. A printed checklist on the wall, the same for everyone, works much better. The only step left is that each new cook must be shown where it hangs.
Option 1: A Committed Folder and core.hooksPath
core.hooksPath tells Git to look for hooks in another folder instead of .git/hooks/. If that folder is inside the repository and committed, everyone has the same hooks:
git init --bare --quiet fee-tracker.git
git clone --quiet fee-tracker.git navika
cd navika
mkdir .githooks
cat > .githooks/commit-msg <<'EOF'
#!/bin/sh
pattern='^(feat|fix|docs|refactor|test|chore)(\([a-z-]+\))?!?: .+'
if ! head -1 "$1" | grep -Eq "$pattern"; then
echo "commit-msg: start the subject with a type, such as 'feat: '."
exit 1
fi
EOF
chmod +x .githooks/commit-msg
git config core.hooksPath .githooks
git add .githooks
git commit -m "Add shared hooks"warning: You appear to have cloned an empty repository.
commit-msg: start the subject with a type, such as 'feat: '.The clone warning is expected, because the stand-in server is still empty. The hook already works for Navika, and it checked the commit that adds it. With a proper message:
git commit --quiet -m "chore: add shared hooks"
git push --quiet origin main
git ls-files.githooks/commit-msgThe hook file is committed, with its executable bit: Git stores "executable" in the tree (mode 100755), so the file stays runnable in every clone on macOS and Linux.
The Missing Step in a New Clone
Advitha clones the repository. She gets .githooks/, but the hook doesn't run, because the setting core.hooksPath is local configuration, and configuration is never cloned either:
cd ..
git clone --quiet fee-tracker.git advitha
cd advitha
git config user.name "Advitha Anamaneni"
git config user.email "advitha@example.com"
echo "Fee Tracker" > README.md
git add README.md
git commit --quiet -m "added readme"
git log --oneline -12cc2b57 (HEAD -> main) added readmeThe badly formatted message went through. One command, run once in each clone, connects the shared folder:
git config core.hooksPath .githooks
git commit --quiet --amend -m "readme"commit-msg: start the subject with a type, such as 'feat: '.Now the hook runs and refuses the message. That one setup command is what every approach below automates.
Making the Setup Automatic
| Approach | How the setup happens |
|---|---|
| Documented step | CONTRIBUTING.md says: run git config core.hooksPath .githooks after cloning |
| Setup script | A scripts/setup.sh (and setup.bat) that sets core.hooksPath and installs other tools |
| Build step | The build runs the command, for example a Maven exec-maven-plugin execution or a Gradle task bound to an early phase, so the first build installs the hooks |
| Hook manager | A tool that installs and runs hooks from a config file (below) |
Option 2: Hook Managers
Hook managers keep the list of checks in a committed config file, install the actual hook scripts with one command, and often handle running checks only on changed files, in parallel, and in the right language environment:
# .pre-commit-config.yaml (install: pip install pre-commit, then: pre-commit install)
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-merge-conflict
- id: detect-private-key# lefthook.yml (install: brew install lefthook or npm i -D lefthook, then: lefthook install)
pre-commit:
commands:
format:
run: mvn -q spotless:check
commit-msg:
commands:
conventional:
run: grep -Eq '^(feat|fix|docs|refactor|test|chore)(\(.+\))?!?: ' {1}# For projects that use npm (install: npx husky init)
# Husky creates a .husky/ folder, sets core.hooksPath to it,
# and adds a "prepare" script to package.json, so that
# "npm install" sets up the hooks in every clone automatically.
# Hooks are plain scripts, for example .husky/pre-commit:
npm test| Tool | Written in | Fits |
|---|---|---|
| pre-commit | Python | Any language. A huge library of ready-made checks |
| Lefthook | Go (single binary) | Any language, fast, runs checks in parallel |
| Husky | JavaScript | Projects that already use npm |
For a Java project, Lefthook or pre-commit avoid adding Node.js just for hooks, while a Maven or Gradle step that sets core.hooksPath needs no extra tool at all.
Personal Hooks for All Your Repositories
core.hooksPath also works globally: git config --global core.hooksPath ~/.git-hooks runs your own hooks in every repository. Note that it then replaces each repository's .git/hooks/ folder, and a repository's local core.hooksPath setting in turn overrides yours. Personal global hooks suit checks you want everywhere, such as a secret scanner.
Why CI Must Check Anyway
Shared hooks make good habits automatic, but they remain optional on each computer: a teammate may not have run the setup, may use --no-verify, or may commit from a tool that doesn't run hooks. So every rule that matters is checked again in CI, on every push and pull request, and protected branches require those checks to pass ("Git and CI/CD"). Hooks give fast feedback before the push. CI gives the guarantee.
Common Mistakes
Committing hooks into .git/hooks/. That folder is inside .git, so nothing there can be committed. Use a normal folder.
Losing the executable bit on Windows. Files created on Windows aren't marked executable. Run git update-index --chmod=+x .githooks/<hook> before committing.
Forgetting the setup step in a new clone. The hooks folder is there, but nothing runs. Automate the setup.
A global core.hooksPath that silently disables project hooks. Your global folder replaces .git/hooks/ everywhere. Projects with their own core.hooksPath still work.
Relying on hooks alone. They can be skipped. Enforce important rules in CI.
Under the Hood
When Git needs a hook, it reads core.hooksPath through the normal configuration order (system, global, local), and if set, looks for the hook there, otherwise in $GIT_DIR/hooks. A relative path such as .githooks is resolved from the top folder of the working tree, so the same setting works in every clone. Hook managers either set core.hooksPath to their own folder (Husky) or write small scripts into .git/hooks/ that call the manager (pre-commit, Lefthook), which then reads its config file and runs the configured checks.
Interview Corner
Why aren't Git hooks shared when you clone a repository?
Hooks live in .git/hooks/, which isn't part of the repository's content, and Git deliberately never installs executable code from a clone, for security.
How do you share hooks with a team?
Commit the hooks in a normal folder (such as .githooks/) and have every clone run git config core.hooksPath .githooks, documented or automated by a setup script or build step. Alternatively, use a hook manager such as pre-commit, Lefthook or Husky.
What does core.hooksPath do?
It tells Git which folder to load hooks from, instead of .git/hooks/. It can be set per repository (for shared hooks) or globally (for personal hooks).
If hooks are shared, why do you still need CI checks?
Hooks run on each developer's computer and can be skipped (--no-verify) or not installed. CI runs the checks on the server for every push and pull request, and branch protection can require them, so they can't be bypassed.
Practice
Challenge: share a pre-commit hook. In Advitha's clone, add a shared pre-commit hook in .githooks/ that rejects files containing DEBUG, commit it with a proper message, and then try to commit a file containing DEBUG.
What does Git print? Before you open the solution, decide whether the new hook also checks the commit that adds it, which contains the word DEBUG too.
Show solution
cat > .githooks/pre-commit <<'EOF'
#!/bin/sh
# Check staged changes, except the hooks folder itself (it mentions DEBUG).
if git diff --cached -U0 -- . ':!.githooks' | grep -q "^+.*DEBUG"; then
echo "pre-commit: remove DEBUG lines before committing."
exit 1
fi
EOF
chmod +x .githooks/pre-commit
git commit --quiet --amend -m "docs: add the README"
git add .githooks/pre-commit
git commit --quiet -m "chore: reject debug output"
echo "DEBUG on" > debug.txt
git add debug.txt
git commit -m "chore: debug settings"pre-commit: remove DEBUG lines before committing.The commit that adds the hook passed, although the hook's own text contains the word DEBUG: the pathspec ':!.githooks' ("everything except the .githooks folder") keeps the check away from the hooks themselves. Without it, the hook would block its own commit. The next commit, with DEBUG on in debug.txt, was refused, because the hook became active the moment its file existed in the hooks folder.
Checkpoint. In a team repository (or two clones of a practice one), share a commit-msg hook through .githooks/, document the setup command in CONTRIBUTING.md, and test it in a fresh clone. Then try one hook manager and compare.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git config core.hooksPath .githooks | Use the committed hooks folder |
git update-index --chmod=+x .githooks/<hook> | Mark a hook executable (from Windows) |
git config --global core.hooksPath ~/.git-hooks | Use personal hooks everywhere |
pre-commit install / lefthook install / npx husky init | Install a hook manager's hooks |
Was this tutorial helpful?