Vasu Patel logoVasu Patel
← Git Mastery
All chapters in this track
  1. 011. The Mental Model
  2. 022. Essential Setup
  3. 033. Core Daily Workflow
  4. 044. Branching
  5. 055. Merging & Conflict Resolution
  6. 066. Rebasing
  7. 077. Remote Repositories
  8. 088. Stashing
  9. 099. Undoing Things
  10. 1010. Tagging & Versioning
  11. 1111. Cherry-Pick
  12. 1212. Git Bisect & Blame
  13. 1313. Git Hooks
  14. 1414. Submodules
  15. 1515. Team Workflows
  16. 1616. Power Commands & Aliases
  17. 1717. Reflog: The Safety Net
  18. 1818. Practice Tasks
  19. 19Quick Reference Cheatsheet

Chapter 13 of 19

13. Git Hooks

Hooks are scripts Git runs automatically at certain events. Located in .git/hooks/.

Most useful hooks

HookRuns whenCommon use
pre-commitBefore commit is createdLint, format, run tests
commit-msgAfter writing commit messageValidate conventional commits format
pre-pushBefore git pushRun full test suite
post-mergeAfter git pull/mergeRun npm install if package.json changed

Example: pre-commit lint check

# .git/hooks/pre-commit  (chmod +x this file)
#!/bin/sh
echo "Running linter..."
npm run lint
if [ $? -ne 0 ]; then
  echo "Lint failed. Commit aborted."
  exit 1
fi

Sharing hooks with the team

.git/hooks/ is not committed. To share hooks:

# Option 1: store in repo, configure path
git config core.hooksPath .githooks

# Option 2: use a tool like Husky (JS projects)
npm install husky --save-dev
npx husky init