Lesson Completion
Back to course

Writing a Simple Hook

Beginner
8 minutesโ˜…4.8Git

The Hook (The "Byte-Sized" Intro)

A Git hook is just a script. Not a plugin. Not a framework. A plain script in .git/hooks/ with the right name and chmod +x. A 3-line pre-commit hook can prevent you from ever committing a console.log again. Let's write one from scratch โ€” no tools, no dependencies, just a shell script.

๐Ÿ“– What is Writing a Simple Hook?

Creating a hook from scratch: choosing the hook event, writing the script, making it executable, and testing it.

Conceptual Clarity

The anatomy of a hook:

PartWhat It IsExample
ShebangTells the OS what interpreter to use#!/bin/sh
LogicThe checks or actions to performnpm run lint
Exit codeDetermines if the action proceedsexit 0 (allow) / exit 1 (block)

Hook creation checklist:

  1. Create the file: .git/hooks/<hook-name> (no extension)
  2. Add shebang line: #!/bin/sh
  3. Write your logic
  4. Make it executable: chmod +x .git/hooks/<hook-name>
  5. Test it: trigger the Git event

Real-Life Analogy

Writing a hook is like programming a door sensor. Choose when it triggers (opening the door = committing), what it checks (did you grab your keys?), and what happens if the check fails (the door stays locked).

Visual Architecture

flowchart TD CREATE["1. Create file<br/>.git/hooks/pre-commit"] --> WRITE["2. Write script<br/>shebang + logic"] WRITE --> CHMOD["3. Make executable<br/>chmod +x"] CHMOD --> TEST["4. Test it<br/>git commit"] style CREATE fill:#0f3460,stroke:#53d8fb,color:#53d8fb style TEST fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • No dependencies: Hooks are plain scripts โ€” no tools needed.
  • Any language: Bash, Python, Node, Ruby โ€” anything executable.
  • Immediate effect: Create the file, make it executable, done.
  • Foundation: Understanding raw hooks helps you use tools like Husky effectively.

Code

bash
# โ”€โ”€โ”€ Hook 1: Block console.log โ”€โ”€โ”€ cat > .git/hooks/pre-commit << 'EOF' #!/bin/sh # Prevent committing console.log statements if git diff --cached --name-only | xargs grep -l "console.log" 2>/dev/null; then echo "โŒ Remove console.log before committing!" echo " Found in the files above." exit 1 fi echo "โœ… No console.log found. Proceeding." EOF chmod +x .git/hooks/pre-commit # โ”€โ”€โ”€ Hook 2: Enforce conventional commit message โ”€โ”€โ”€ cat > .git/hooks/commit-msg << 'EOF' #!/bin/sh MSG=$(cat "$1") if ! echo "$MSG" | grep -qE "^(feat|fix|chore|docs|refactor|test): "; then echo "โŒ Message must start with: feat|fix|chore|docs|refactor|test" echo " Got: $MSG" exit 1 fi EOF chmod +x .git/hooks/commit-msg # โ”€โ”€โ”€ Hook 3: Run tests before push โ”€โ”€โ”€ cat > .git/hooks/pre-push << 'EOF' #!/bin/sh echo "๐Ÿงช Running tests before push..." npm test EOF chmod +x .git/hooks/pre-push # โ”€โ”€โ”€ Test the hook โ”€โ”€โ”€ echo "console.log('debug')" >> test.js git add test.js git commit -m "test" # โŒ Remove console.log before committing!

Key Takeaways

  • Hooks are just scripts โ€” no framework or tools needed.
  • Three requirements: correct name, shebang line, executable permission.
  • exit 0 allows the action; exit 1 blocks it with an error message.
  • git diff --cached is the key to checking only staged files.

Interview Prep

  • Q: How do you create a Git hook from scratch? A: Create a file in .git/hooks/ named after the hook event (e.g., pre-commit), add a shebang line, write your check logic, and make it executable with chmod +x.

  • Q: How does a pre-commit hook check only staged files? A: Using git diff --cached --name-only to get the list of staged files, then running checks (lint, grep) only on those files.

  • Q: Can hooks be written in any language? A: Yes โ€” any executable script works. Use the appropriate shebang: #!/bin/sh for shell, #!/usr/bin/env python3 for Python, #!/usr/bin/env node for Node.js.

Topics Covered

Git HooksScripting

Tags

#git#hooks#scripting#beginner

Last Updated

2026-02-13