2021-10-20    Share on: Twitter | Facebook | HackerNews | Reddit

Git Hooks

Git hook makes it easier to keep order in the software project:

  • can help following agreed conventions related with e.g.
    • code format, code quality
    • workflow
    • commit practices and format

Exemplary check before the commit

If not exists, create hooks directory in the .git folder of the demo and add these files.

.git/hooks
├── isort_hooks.py
└── pre-commit

isort_hooks.py

#!/usr/bin/env python
import sys

from isort.hooks import git_hook

sys.exit(git_hook(strict=True, modify=True))

pre-commit

set -e # stop script execution when error encoutered

# Run flake8 check
flake8 .

# Sort imports with isort
python ./.githooks/isort_hooks.py

Alternatives

There is a tool pre-commit that eases the installation and configuration of git hooks. Read my note on that: Pre-commit hooks

References:

Sharing git hooks with the team

Nowadays you can do the following to set a directory that is under version control to be your git hooks directory, e.g., MY_REPO_DIR/.githooks would be

git config --local core.hooksPath .githooks/

Still not directly enforceable but, if you add a note in your README (or whatever), this requires a minimum of effort on each developer's part. Source: Stackoverflow

See also:

]