Your pre-commit hook does not git add
the updated test.txt
file.
Note that the version of the file in the working directory is not necessarily the version that would be committed:
echo foo > test.txt
git add test.txt
echo bar > test.txt
git commit -m 'this commit has "foo" in test.txt'
To see, in a pre-commit hook, the version of the file in the index—i.e., what will be committed if the pre-commit hook allows the commit to proceed, without doing any git add
step—you can use git show :0:test.txt
. The magic :0:
prefix is gitrevisions syntax for "version staged for commit". (You can abbreviate this further as just :test.txt
, although I find the :0:
a bit more obvious as a revspec; I've seen real files really named :README
for instance. :-) ) Note that this bypasses any smudge filter(s) though.
(Hooks that want to test the entire current index contents, but preserve working directory state as well, can use git stash save --keep-index
. See How do I properly git stash/pop in pre-commit hooks to get a clean working tree for tests for advice, and a caveat, on doing this.)
In my current version of git (1.8.5.4), if I modify a file and git add
the result in a pre-commit hook, the contents I add
ed are what goes into the commit. (I do have some vague recollection of an earlier version of git saving the index before the pre-commit hook runs, so that changes made to the index did not make it into the commit, but I might just be mis-remembering.)
Aside: It's generally not a great idea to modify the commit-contents in a pre-commit hook. They're really just meant to verify that the commit is ready to go. But if you're determined to modify the contents, git add
(or git update-index
but add is much simpler) will do it.