Git Notes
Learn to use Git hooks for automating code quality checks, commit message validation, pre-push testing, and deployment triggers. Covers client-side and server-side hooks.
Git hooks are scripts that run automatically at specific points in the Git workflow. They enable automation for code quality checks, commit message validation, testing, and deployment.
What Are Git Hooks?
Hooks are executable scripts stored in .git/hooks/ that Git triggers during operations like committing, pushing, or merging.
| │ git commit | │ |
| │ pre-commit | prepare-commit-msg → │ |
| │ commit-msg | post-commit │ |
| │ git push | │ |
| │ pre-push | (remote) pre-receive → │ |
| │ update | post-receive │ |
| │ git merge | │ |
| │ pre-merge-commit | post-merge │ |
Client-Side Hooks
pre-commit
Runs before the commit message editor opens. Use for linting and formatting:
commit-msg
Validates the commit message format:
#!/bin/sh
# .git/hooks/commit-msg
COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
# Enforce conventional commits format
PATTERN="^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,72}$"
if ! echo "$COMMIT_MSG" | grep -qE "$PATTERN"; then
echo "❌ Invalid commit message format!"
echo ""
echo "Expected: type(scope): description"
echo "Types: feat, fix, docs, style, refactor, test, chore"
echo ""
echo "Example: feat(auth): add login functionality"
echo "Your message: $COMMIT_MSG"
exit 1
fi
echo "✅ Commit message format valid"
exit 0pre-push
Runs before pushing to remote. Use for running tests:
prepare-commit-msg
Modifies the default commit message:
post-commit
Runs after a commit is created (cannot prevent the commit):
#!/bin/sh
# .git/hooks/post-commit
echo "✅ Commit created successfully!"
echo "Commit: $(git log --oneline -1)"Server-Side Hooks
pre-receive
Runs on the remote server before accepting a push:
post-receive
Runs after a push is accepted. Used for deployment triggers:
Setting Up Hooks
Manual Setup
# Navigate to hooks directory
cd .git/hooks
# Create a hook (example: pre-commit)
cat > pre-commit << 'EOF'
#!/bin/sh
echo "Running pre-commit hook..."
npm run lint
EOF
# Make it executable
chmod +x pre-commitUsing Husky (Recommended for Teams)
Husky manages Git hooks through package.json, so hooks are shared with the team:
# Install Husky
npm install --save-dev husky
# Initialize Husky
npx husky init# Create a pre-commit hook
echo "npm run lint" > .husky/pre-commit
# Create a commit-msg hook
echo 'npx commitlint --edit $1' > .husky/commit-msg// package.json
{
"scripts": {
"prepare": "husky",
"lint": "eslint src/",
"test": "jest"
}
}Using lint-staged
Run linters only on staged files for speed:
npm install --save-dev lint-staged# .husky/pre-commit
npx lint-stagedAll Available Hooks
| Hook | Trigger | Can Abort? |
|---|---|---|
| pre-commit | Before commit | ✅ Yes |
| prepare-commit-msg | After default msg created | ✅ Yes |
| commit-msg | After user enters message | ✅ Yes |
| post-commit | After commit created | ❌ No |
| pre-rebase | Before rebase starts | ✅ Yes |
| post-rewrite | After commit rewrite (amend/rebase) | ❌ No |
| pre-push | Before push to remote | ✅ Yes |
| pre-merge-commit | Before merge commit | ✅ Yes |
| post-merge | After merge completes | ❌ No |
| post-checkout | After checkout/switch | ❌ No |
Bypassing Hooks
# Skip pre-commit and commit-msg hooks
git commit --no-verify -m "Emergency fix"
# or
git commit -n -m "Emergency fix"
# Skip pre-push hook
git push --no-verifyInterview Questions
- What are Git hooks and where are they stored?
Git hooks are scripts that run automatically at specific points in the Git workflow. They are stored in the .git/hooks/ directory of each repository. Hooks can automate tasks like linting, testing, and deployment.
- How do you share hooks with your team?
Use tools like Husky that store hook configurations in the repository (e.g., .husky/ directory). Since .git/hooks/ is not tracked, you need a mechanism to install hooks when teammates clone the repo.
- What is the difference between pre-commit and commit-msg hooks?
pre-commit runs before the commit message editor opens and is used for code checks (linting, formatting). commit-msg runs after the message is written and validates the commit message format.
- How do you bypass a Git hook?
Use the --no-verify or -n flag: git commit --no-verify. This skips pre-commit and commit-msg hooks. Use sparingly for emergencies.
- What is the difference between client-side and server-side hooks?
Client-side hooks run on the developer machine (pre-commit, pre-push) and can be bypassed. Server-side hooks run on the remote (pre-receive, post-receive) and cannot be bypassed by clients.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Hooks - Automate Workflows with Git Events.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, advanced, hooks, git hooks - automate workflows with git events
Related Git & GitHub Topics