Pre-commit
Last updated on 2025-03-12 | Edit this page
Overview
Questions
- What quality assurance checks can I run using pre-commit checks?
Objectives
- Explain what pre-commit hooks are.
- Install and run a pre-commit hook.
Git hooks let you run certain scripts before git actions. In this optional episode we are going to set up a pre-commit hook which runs quality assurance checks on our code before we are allowed to make a commit.
pre-commit is a Python package that makes it easier to set up git hooks. To install it using pip or conda:
In the git-training-demo
we provided an example
pre-commit configuration. These pre-commit checks were run as part of
your pull requests. We can install the pre-commit hook scripts so they
run before we can commit locally:
OUTPUT
pre-commit installed at .git/hooks/pre-commit
It’s good practice to run the pre-commit checks on all your files after installing them.
OUTPUT
[INFO] Initializing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Installing environment for https://github.com/pre-commit/pre-commit-hooks.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
don't commit to branch ...................................................Passed
check for added large files ..............................................Passed
check for case conflicts .................................................Passed
check for merge conflicts ................................................Passed
check toml ...........................................(no files to check)Skipped
check yaml ...............................................................Passed
fix end of files .........................................................Passed
trim trailing whitespace .................................................Passed
The next time the pre-commit scripts run they will re-use the Python environment that was just installed, making the run faster.
The pre-commit.com site has a reference of useful hooks. Your organisation may have their own pre-commit hooks which you can use and or block third party hooks for security.
Committing to Main
With the pre-commit installed try adding any change to the
main
branch and then run git commit
.
What happens with the pre-commit hooks installed?
The pre-commit check for not committing to main fails so
git commit
wasn’t run. Your changes are still staged.
OUTPUT
don't commit to branch...................................................Failed
- hook id: no-commit-to-branch
- exit code: 1
check for added large files..............................................Passed
check for case conflicts.................................................Passed
check for merge conflicts................................................Passed
check toml...........................................(no files to check)Skipped
check yaml...............................................................Passed
fix end of files.........................................................Passed
trim trailing whitespace.................................................Passed
To move the staged changes to a new branch use
git switch
:
OUTPUT
Switched to a new branch 'challenge'
OUTPUT
On branch challenge
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: .pre-commit-config.yaml
Key Points
- pre-commit checks help prevent accidental commits to
main
and provide an extra layer of quality assurance.