This Git pre-commit hook checks for a keyword ("nocheckin" by default) in staged source files and aborts the commit if any matches are found, with the output of file path, line number and match preview.
This is inspired by Jonathan Blow's nocheckin setup with SVN, as seen on his programming sessions at
Twitch and YouTube.
Additional credit to DustinGadal
on the r/Jai
subreddit for original implementation.
Tested on Linux (GNOME OS 49 aarch64) and macOS arm64.
As a download:
- Download the file from above, either by clicking Download ZIP and extracting it, or by clicking
Raw
and saving the contents to a file. - Copy the
pre-commit-nocheckin
(or saved) file and rename it to<repo>/.git/hooks/pre-commit
. If you already have a pre-commit hook, merge it into the file. - On Unix systems, you will likely need to mark the file as executable by running
chmod +x ./git/hooks/pre-commit
. Git will warn about this when issuing a commit if it's the case, though it will perform that commit without running this hook.
pre-commit
" for Git to recognize and use it as a pre-commit hook.
Manually:
- Copy the contents of the file from above.
- Create
<repo>/.git/hooks/pre-commit
and paste the contents, or merge if exists.
CLI:
- Stage files with
git add <file> ...
- Attempt making a commit with
git commit ...
- Observe the commit failing when at least one of the staged files contains a
nocheckin
.
GUI (Github Desktop)
- Mark files for staging.
- Attempt making a commit.
- Observe the commit failing when at least one of the staged files contains a
nocheckin
.
Changing the keyword
You can change the keyword that's used for rejecting commits by setting the NOCHECKIN_KEYWORD
variable through the environment, or editing the default at the top of the script (DEFAULT_KEYWORD
)
Any case-insensitive occurance of the keyword will reject a commit, whether it is part of a comment or is used within the code.
Environment variables
NOCHECKIN_KEYWORD
: The keyword used for rejecting commits.
Default:nocheckin
NOCHECKIN_ENABLE_AWK_FORMATTING
: Whether to use nicer output formatting when available.
Default:1
NOCHECKIN_MAX_COLUMNS
: How many columns to aim for when using nicer output formatting.
Default:120