Lint commits during creation
Git hooks allow you to run commands before saving a commit, after saving a commit, pushing a branch, etc. You can run Lintje when these things happen to lint your commits.
Use the installer below to add a Git hook that validates the commit right before it’s saved. Run this command in each project you want to use Lintje.
lintje --install-hook commit-msg
If Lintje finds one or more issues, the commit is not saved. The message you entered is still available in the
.git/COMMIT_EDITMSG file. Using this file you can continue writing your commit message with the following command:
git commit --edit --file=.git/COMMIT_EDITMSG
If you do not want Git to stop creating a commit, please read about validating commits after saving a commit.
Personally, I don’t like how the Git
commit-msg hook fails the commit process if any issues are found. It requires extra steps to continue writing the commit with the original message. It also makes making fixup commits impossible, because of the RebaseCommit rule.
Instead, I prefer using the
post-commit hook. If any issues are reported by Lintje, you can amend the commit message afterwards. Along with automated review in Continuous Integration you can make sure no invalid commits get merged.
Use the installer below to add a Git hook that validates the commit right after it’s saved. Run this command in each project you want to use Lintje.
lintje --install-hook post-commit
When Lintje finds any issues, you can then amend the commit to fix the reported issues.
git commit --amend
Git unfortunately does not support global hook that apply to all Git repositories on your machine. You’ll need to use the commands described above to configure existing repositories. For new repositories it’s possible to preconfigure hooks with a Git template.