git pre-commit hook not running when running git commit -a

◇◆丶佛笑我妖孽 提交于 2019-12-13 09:54:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!