The Hook (The "Byte-Sized" Intro)
Hooks live in .git/hooks/ by default — but that's inside .git/, which means they're not tracked by Git. Clone a repo and its hooks don't come with it. This is both a security feature (you don't want random repos running scripts on your machine) and a challenge (teams need shared hooks). The solution? Change where hooks live.
📖 What are Hook Locations?
The default hook directory is .git/hooks/. Git can be configured to use a different directory, enabling hooks to be committed and shared with the team.
Conceptual Clarity
Hook storage locations:
| Location | Tracked? | Shared? | Purpose |
|---|---|---|---|
.git/hooks/ | ❌ | ❌ | Default, local-only |
.githooks/ (custom) | ✅ | ✅ | Team-shared hooks |
| Template dir | ❌ | 🟡 (on init/clone) | Pre-install on new repos |
How to change the hook directory:
git config core.hooksPath .githooks
Now Git looks in .githooks/ (which IS tracked) instead of .git/hooks/.
Requirements for hook files:
- Must be executable (
chmod +x) - Must have correct shebang line (
#!/bin/sh,#!/usr/bin/env python3) - Must be named exactly after the hook event (no extension)
Real-Life Analogy
Default hooks are like notes stuck to your personal desk — only you see them. Moving hooks to a shared directory is like posting the notes on the team board — everyone follows the same rules.
Visual Architecture
Why It Matters
- Default location is not shared — hooks don't transfer on clone.
- Custom
hooksPathenables committing hooks to the repo. - Templates auto-install hooks on
git initandgit clone. - Executable permission is the #1 reason hooks silently fail.
Code
# ─── Default location ───
ls .git/hooks/
# pre-commit.sample commit-msg.sample ...
# ─── Move hooks to a tracked directory ───
mkdir .githooks
mv .git/hooks/pre-commit .githooks/pre-commit
git config core.hooksPath .githooks
# ─── Commit so the team gets them ───
git add .githooks/
git commit -m "chore: add shared Git hooks"
# ─── Team members set up with one command ───
git config core.hooksPath .githooks
# ─── Or automate via a setup script ───
echo 'git config core.hooksPath .githooks' > setup.sh
chmod +x setup.sh
# ─── Template-based approach ───
git config --global init.templateDir ~/.git-templates
mkdir -p ~/.git-templates/hooks
# Place hook scripts here → auto-installed on every new repoKey Takeaways
- Default hooks in
.git/hooks/are local and not tracked. - Use
core.hooksPathto point to a tracked directory like.githooks/. - Hook files must be executable and have the correct shebang line.
- Templates auto-install hooks on
git initandgit clone.
Interview Prep
-
Q: Why don't Git hooks transfer when you clone a repo? A: Because hooks are stored in
.git/hooks/, which is inside the.gitdirectory and not tracked. This is a security measure — cloning a repo shouldn't automatically run arbitrary scripts. -
Q: How do you share hooks with a team? A: Set
core.hooksPathto a tracked directory (e.g.,.githooks/), commit the hooks, and have team members rungit config core.hooksPath .githooks. Or use tools like Husky that automate this. -
Q: What are the requirements for a hook file to work? A: It must be named exactly after the hook event (e.g.,
pre-commit, no extension), must be executable (chmod +x), and must have a valid shebang line (e.g.,#!/bin/sh).