Manuals / Git & GitHub / Chapter 10

D · Automate · advanced · Day 7–8 · Chapter 10 of 12

Git hooks — pre-commit & pre-push

Hooks run scripts at Git lifecycle events. pre-commit: lint/format. pre-push: tests. Husky or plain .git/hooks.

Path progress
78%

Step 1 of 3

Simple pre-commit hook

Executable script in .git/hooks/pre-commit. Exit 1 blocks commit. Test in practice repo.

Simple pre-commit hookDrag stickies · tap for tips
Study mapDrag stickies · tap for tipsKeep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Assert the UIdrag · tap →Retry wiselydrag · tap →Seed datadrag · tap →Close the loopdrag · tap →Keep it shortdrag · tap →Name the waitdrag · tap →Scope locatorsdrag · tap →Trace when stuckdrag · tap →One browser firstdrag · tap →Isolate statedrag · tap →Pathwise hackdrag · tap →Simple pre-commit drag · tap →Try thisdrag · tap →Follow the dashed drag · tap →

Example

#!/bin/sh
# .git/hooks/pre-commit
if ! grep -q "." README.md 2>/dev/null; then
  echo "README must not be empty"
  exit 1
fi

Do this now

Hook that rejects commits if README is empty.

Was this step clear?
Chapter learning outcomes
  • Hook scripts in .git/hooks
  • pre-commit framework
  • Husky for Node projects
  • CI vs local hooks

Clear these before you leave

Side quest

pre-push tests

pre-push hook runs pytest or npm test before push.