-
Notifications
You must be signed in to change notification settings - Fork 287
Setting Up pre‐commit
pre-commit
is a framework for managing and maintaining pre-commit hooks which are scripts that run when you use git commit
. Below are the steps to install pre-commit
using both pip
and conda
, and enable it.
-
make sure you are using the correct virtual environment and run:
pip install pre-commit
-
Install pre-commit:
conda install -c conda-forge pre-commit
Or you can create a new virutal environment from the
environment.yml
as it pre-commit is included there:conda env create -f environment.yaml
-
Install the hooks:
Run the following command to install the hooks defined in your configuration file:
pre-commit install
This will set up the
pre-commit
hooks to run automatically ongit commit
. -
Making a Commit:
After installing
pre-commit
, the hooks will run automatically every time you make a commit. If any hook fails, the commit will be aborted (as if you didn't commit at all). You need to fix the issues reported by the hooks and try committing again. Fortunately, All hooks currently in the repository are just formatters and linting fixers so you should just check which files where changed usinggit status
, make sure they are ok, add them usinggit add
, and finally git commit again usinggit commit
.
The pre-commit hooks we used uses ruff
for formatting and linting. If pre-commit doesn't work make sure that ruff
is properly installed in your virtual environment.
- you can test if pre-commit has been correctly installed by making an empty commit:
git commit --allow-empty -m "testing"
and you should see the following in your terminal:
and then for removing the empty commit you can run git reset --soft HEAD~
.
-
Manually Running Hooks:
If you want to run the hooks on all files (not just the modified ones), use:
pre-commit run --all-files
Or run hooks on just the staged files with:
pre-commit run
Now you have pre-commit
installed and configured in your project, helping to ensure code quality and consistency by running checks automatically before each commit.