问题
I have a local pre-commit hook which executes, and stops the commit as expected when I run
git commit
Also, as expected, I can bypass the hook when I run git commit -n ...
However, running git commit -am "My message"
or indeed, just git commit -a
seems to bypass the hook and allow the commit to be processed.
Any idea why this may be happening?
Edit: Hook below.
PROJECT_ROOT=`git rev-parse --show-toplevel`
CHANGED=`git diff --stat -- $PROJECT_ROOT/myProj/myfile.ext | wc -l`
if [ $CHANGED -gt 0 ];
then
echo "myfile.ext file has changed, are you sure you want to commit"
echo "Use the -n parameter to process this commit."
exit 1
fi
回答1:
So, it was user error..
The hook was running in both instances, however the logic I used to detect a change in my file didn't work when the -a command was supplied.
Checking against the remote repo did the trick.
CHANGED=`git diff origin/master --stat -- $PROJECT_ROOT/myProj/myfile.ext | wc -l`
edit: thanks to @torek, a more appropriate way to check for a change is:
CHANGED=`git diff --cached --stat -- $PROJECT_ROOT/myProj/myfile.ext | wc -l`
来源:https://stackoverflow.com/questions/45749689/git-pre-commit-hook-not-running-when-running-git-commit-a