A pre-commit hook runs an automated check before Git creates a commit. A common use is running tests so failed tests prevent the commit and problems are caught early. section 11.7
Set up a basic test-running hook in the repository:
- Create a file named pre-commit inside .git/hooks/. section 11.7
- Add a shell command that runs pytest and exits when tests fail. section 11.7
- Make the hook executable with chmod +x .git/hooks/pre-commit. section 11.7
A test-gating hook
A hook containing `pytest -v || exit 1` runs the test suite before each commit. Passing tests allow the commit; a failure stops it. Run only the tests needed during development when slower integration or end-to-end tests use external services, delays, or costs. section 11.7
#!/bin/bash pytest -v || exit 1
The listing shows a minimal shell hook that runs pytest verbosely and prevents continuation after failure. section 11.7
