Unfortunately, there is not. However, there is a workaround.
Git lets you filter files before staging (and before checkout), if needed. You can hook right there. See the filter section in the docs
For example, lets say, all .txt files may not contain the word donotcommit
. First, we create the filter in .git/config
or in your global config file:
[filter "donotcommit"]
clean = bash -c 'tee >(cat) | grep donotcommit -qi && exit 1 || exit 0'
smudge = cat
required
The clean filter is run on add
, the smudge filter on checkout. required
tells git that whenever this command fails, the file is in a bad state and can not be added. The command receives the file-contents piped to stdin, and since it's a filter and what it outputs gets added as file-content, we need to make sure to pass the whole file through, thats what the tee >(cat)
in clean
and the cat
in smudge is for. The | grep donotcommit -q && exit 1 || exit 0
part should be clear: Look for donotcommit
(case insensitive), if found, exit with an error (1), otherwise exit cleanly (0).
Then, register this filter for txt-files in the repositories .gitattributes
file
*.txt filter=donotcommit
Result:
C:\dev\test>echo Hello World > test.txt
C:\dev\test>git add test.txt
C:\dev\test>echo Hello World [DONOTCOMMIT] > test.txt
C:\dev\test>git add test.txt
error: external filter 'bash -c 'tee >(cat) | grep donotcommit -qi && exit 1 || exit 0'' failed 1
error: external filter 'bash -c 'tee >(cat) | grep donotcommit -qi && exit 1 || exit 0'' failed
fatal: test.txt: clean filter 'donotcommit' failed
C:\dev\test>