Skip to content

Hook

In one line

Git hooks are custom scripts that Git automatically executes at specific points in the workflow, like before a commit or after a merge.

In simple words

Git hooks are scripts stored in .git/hooks/ that Git runs automatically at specific events. They let you enforce policies, run tests, format code, or send notifications without manual intervention.

Common hooks: pre-commit (runs before a commit is created — use to lint or run tests), pre-push (runs before a push — use to prevent pushing broken code), post-merge (runs after a merge — use to install dependencies), and commit-msg (runs to validate commit message format).

Hooks are local to each repository and are not version-controlled by default (they live in .git). Teams often use tools like Husky (for JavaScript projects) to share hooks via package.json. Hooks can be any executable script — shell, Python, Node.js, etc.

Example

bash
# Sample .git/hooks/pre-commit
#!/bin/sh
# Run tests before allowing commit
npm test
if [ $? -ne 0 ]; then
  echo "Tests failed — commit aborted"
  exit 1
fi

# Make it executable
chmod +x .git/hooks/pre-commit

pre-commit hook runs tests before each commit, aborting on failure.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary