Pre-commit Hooks: Your Code's Quality Gatekeeper
Think of a pre-commit hook as a bouncer for your codebase. It's a script that runs before Git finalizes a commit, checking your changes against project rules. Use it to automatically lint code, run formatters, or check for secrets before they enter history.
Why it exists
To prevent simple mistakes and enforce coding standards before they are recorded in the project's history. Manually running linters, formatters, and other checks is error-prone. Automating this process at the commit stage catches issues early and maintains code quality.
The mental model
A pre-commit hook is a script that acts as a quality gate for your git commit command. Before Git saves your changes, it runs this script. If the script finishes successfully (exits with code 0), the commit proceeds. If it fails (exits with a non-zero code), Git aborts the commit, forcing you to fix the issues. It's a personal, local safety net.
How it works
When you run git commit, Git checks for an executable file named pre-commit inside the local .git/hooks/ directory. If found, Git executes it. The script can inspect all the files you've staged for the commit. Common tasks include running linters (like Stylelint for CSS), code formatters (like Prettier), or custom scripts. If any of these checks fail, the script exits with a non-zero status code, and Git prints the script's output to your terminal and cancels the commit.
When to use it
Use pre-commit hooks for fast, automated checks that provide immediate feedback to a developer. This is ideal for linting CSS or JavaScript, formatting code to a consistent style, or running quick unit tests relevant to the changed files. It helps keep the main branch clean from trivial errors and style inconsistencies.
When not to use it
Don't use pre-commit hooks for long-running tasks like a full integration test suite, which would make committing painfully slow. Also, because hooks are not automatically shared when a repository is cloned, they cannot be the only enforcement mechanism. Critical checks must also be run in a shared CI/CD pipeline to ensure all code meets quality standards.
One canonical example
In a design system project, a hook might run prettier --write on all staged .css and .js files to enforce formatting, followed by stylelint to check for CSS rule violations. If Stylelint finds an error, it will exit non-zero, blocking the commit and showing the developer exactly which CSS rule was broken.
Interview question
Which task is generally considered unsuitable for a Git pre-commit hook?
- a.Running a linter to identify style violations or potential bugs.
- b.Verifying that no sensitive API keys are accidentally staged for commit.
- c.Executing a comprehensive suite of integration tests for the entire application.Correct
- d.Automatically reformatting code to a consistent style.
Why? this is the answer
Pre-commit hooks are intended for fast, local checks to provide immediate feedback. Long-running tasks like a full integration test suite would significantly slow down the commit process, making it frustrating for developers. The other options are all examples of quick, automated checks suitable for a pre-commit hook.
Just read this? Test yourself on what you have been reading.
Read the original → git-scm.com
- #git
- #tooling
- #css
- #linting
- #automation
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on git — each one lists the topics its interview covers.
See open roles