Humans are terrible at repetitive tasks. We forget to run linters. We forget to check for trailing whitespace. We forget to run the test suite before pushing.
Computers, on the other hand, love repetition. This is where Git Hooks come in.
What Are Git Hooks? Automated Pre-Commit Checks Explained
Git Hooks are scripts that Git executes automatically before or after specific repository events β such as commit, push, and receive β letting you βhookβ into the Git workflow and stop bad commits before they happen.
The most useful one for daily development is the pre-commit hook.
What Is a Git Pre-Commit Hook?
A pre-commit hook is a script that runs every time you type git commit. If the script exits with an error (non-zero status), Git aborts the commit.
Itβs the perfect place to put comprehensive checks, naming the specific failure modes it catches:
- Broken syntax or code that fails to compile
- Lingering debug artifacts, such as stray
print()statements - Formatting violations against the teamβs style guide (line length, indentation, trailing whitespace)
- YAML/JSON syntax errors in config files
How to Install and Configure a Pre-Commit Hook
You can write bash scripts directly in .git/hooks/, but thatβs hard to share with a team. I recommend using the pre-commit framework β an open-source tool that manages and runs Git hook scripts defined in a shared, version-controlled YAML config file.
- Install it:
pip install pre-commit - Add a config file
.pre-commit-config.yamlto your repo:
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- Install the git hook scripts:
pre-commit install
Now, whenever you commit, these little robots will run. If you left trailing whitespace, they will fix it for you and stop the commit so you can stage the clean version.
Fixing pre-commit: command not found
Cause: The pre-commit executable installed by pip install pre-commit isnβt on your shellβs PATH β common when pip installs to a user-local bin directory that isnβt linked yet.
Fix: Run python -m pip install --user pre-commit, then add the reported user base bin directory to your PATH (or install with pipx via pipx install pre-commit, which handles the PATH setup for you). Confirm it worked with pre-commit --version.
Why Use Git Hooks: Shifting the Feedback Loop Left
Git Hooks shift the feedback loop left. Instead of waiting 10 minutes for your CI/CD pipeline to fail on GitHub, you find out instantly on your own machine.
Git Hooks save time, embarrassment, and keep the main codebase pristine.
Related tools you may want to try next.
Mixo.io generates websites instantly using AI. Builds stunning landing pages without any code or design. Includes a built-in email waiting list and all the tools you need to launch, grow, and test your ideas.
Git Hooks Summary: Automating Code Quality Checks
Git Hooks represent an automation layer that enforces code-quality standards before code ever reaches CI/CD. Set up a pre-commit hook today, and let the robot butler handle the boring stuff while you focus on solving interesting problems.
Git Pre-Commit Hooks FAQ
What is a Git pre-commit hook?
A pre-commit hook is a script that Git runs automatically every time you type git commit. If the script exits with a non-zero status, Git aborts the commit, so you can catch problems like trailing whitespace, formatting violations, or failing checks before bad code ever enters your history.
How do I install pre-commit hooks in Git?
Install the framework with pip install pre-commit, add a .pre-commit-config.yaml file listing the hooks you want (for example trailing-whitespace, end-of-file-fixer, check-yaml), then run pre-commit install to wire the hooks into your local .git/hooks/ directory.
Can I skip a Git hook temporarily?
Yes β run git commit --no-verify (or -n) to bypass all pre-commit hooks for a single commit. Use this sparingly; skipping hooks defeats the purpose of automated quality checks.
What happens if a pre-commit hook fails?
Git aborts the commit and shows which hook failed. Some hooks (like trailing-whitespace and end-of-file-fixer) automatically fix the issue and re-stage the file, so you just need to run git commit again; others require you to fix the reported issue manually before retrying.
Did you like this post? Please let me know if you have any comments or suggestions.
Git posts that might be interesting for youEnjoyed this? Get more like it.
Weekly notes on AI tools, Python, and what I'm actually building β plus a free copy of Fantastic AI: The 2026 Toolkit.