Skip to content

Instantly share code, notes, and snippets.

@gbourant
Forked from xezrunner/pre-commit-nocheckin
Created February 24, 2025 21:13
Show Gist options
  • Save gbourant/3652e490f2999a6cb4761e5d017c831d to your computer and use it in GitHub Desktop.
Save gbourant/3652e490f2999a6cb4761e5d017c831d to your computer and use it in GitHub Desktop.
nocheckin pre-commit hook for Git
#!/bin/bash
# Author: xezrunner (github.com/xezrunner)
# Inspired by Jonathan Blow's nocheckin setup with SVN (twitch.tv/j_blow) (youtube.com/@jblow888)
# Credit: DustinGadal on r/Jai: reddit.com/r/Jai/comments/jp0vjy/nocheckin_behavior_in_gitsourcetree/gbfhzd1
# Required programs/utilities: git, grep, xargs, wc
# To install, move and rename to <repo>/.git/hooks/pre-commit (or merge)
# The file name has to be "pre-commit" for Git to use it. Compatible with Github Desktop GUI.
# This pre-commit hook checks for the existence of the word "$SEARCH_TARGET"
# in your source files, then aborts the commit if any matches were found, with
# output of file path, line number and match preview.
SEARCH_TARGET="nocheckin"
# Reject colors when not supported by terminal (/or GUIs like Github Desktop):
ncolors=$(tput colors)
if test -t 1 && test -n "$ncolors" && test $ncolors -ge 8; then
CL_RED='\033[31m'
CL_BRED='\033[1;31m'
CL_NONE='\033[0m'
fi
MESSAGE_0="${CL_BRED}Error:${CL_NONE} $SEARCH_TARGET(s) in"
MESSAGE_1="file(s):"
SEARCH_CMD="git diff --staged -i --diff-filter=d --name-only -G $SEARCH_TARGET --relative"
GREP_CMD="grep -H $SEARCH_TARGET -n --color=auto" # <filename>:line******
# 'wc -l' (line count of input) to get number of files from diff output:
STATUS=$($SEARCH_CMD | wc -l)
if ((STATUS > 0)); then
echo -e $MESSAGE_0 $STATUS $MESSAGE_1;
($SEARCH_CMD | xargs $GREP_CMD);
exit 1;
fi

nocheckin pre-commit hook for Git

This Git pre-commit hook checks for the existence of the word "nocheckin" ($SEARCH_TARGET) in staged source files, then aborts the commit if any matches were found, with 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.

Credit to DustinGadal on the r/Jai subreddit for original implementation.

Tested on Linux and macOS.

Installation

Download the file from above.
Move the file and rename it to <repo>/.git/hooks/pre-commit (or merge manually if you already have a pre-commit hook).
The file name has to be "pre-commit" for Git to use it.

Once installed, it is compatible with the Github Desktop GUI as well.

Usage

CLI:

  1. Stage files with git add <file> ...
  2. Attempt making a commit with git commit ...
  3. Observe the commit failing with the appropriate output when at least one of the staged files contains a nocheckin.

GUI (Github Desktop)

  1. Mark some files for staging.
  2. Attempt commiting.
  3. Observe the commit failing with the appropriate output when a file contains a nocheckin.

Screenshots

Command-line example: image

GitHub Desktop GUI example: image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment