Lesson Completion
Back to course

Hook Locations

Beginner
7 minutes4.7Git

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:

LocationTracked?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

flowchart TD DEFAULT[".git/hooks/<br/>Not tracked"] --> CUSTOM[".githooks/<br/>Tracked in repo"] CUSTOM --> SHARED["✅ Shared with team"] TEMPLATE["Template dir"] --> CLONE["Auto-installs on clone"] style DEFAULT fill:#2d1b1b,stroke:#e94560,color:#e94560 style CUSTOM fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb style SHARED fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Default location is not shared — hooks don't transfer on clone.
  • Custom hooksPath enables committing hooks to the repo.
  • Templates auto-install hooks on git init and git clone.
  • Executable permission is the #1 reason hooks silently fail.

Code

bash
# ─── 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 repo

Key Takeaways

  • Default hooks in .git/hooks/ are local and not tracked.
  • Use core.hooksPath to point to a tracked directory like .githooks/.
  • Hook files must be executable and have the correct shebang line.
  • Templates auto-install hooks on git init and git 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 .git directory 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.hooksPath to a tracked directory (e.g., .githooks/), commit the hooks, and have team members run git 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).

Topics Covered

Git HooksConfiguration

Tags

#git#hooks#locations#config

Last Updated

2026-02-13