git pre-push hook似乎不适用于git push

时间:2019-01-16 22:11:01

标签: git githooks

我正在考场上

$git branch
  master
* testbranch

这是我在.git/hooks/pre-push文件中的代码:

#!/bin/bash
protected_branch='testbranch'
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

if [ $protected_branch = $current_branch ]
    then
            echo "Test"
            pwdlsfail

            rc=$?
            if [[ $rc != 0 ]] ; then
                echo "test failed - push denied. Run tests locally and confirm they pass before pushing"
                exit $rc
            fi
else
# Everything went OK so we can exit with a zero
    exit 0
fi

尝试将以上代码作为shell脚本运行,并且运行良好:

$./1.sh  
Test
./1.sh: line 8: pwdlsfail: command not found
test failed on rev  - push denied. Run tests locally and confirm they pass before pushing

但是pre-push钩子仍然没有被git push origin testbranch调用,我丢失了什么吗?

1 个答案:

答案 0 :(得分:1)

从您的描述中可以看出,您正在尝试将pre-receive钩子放入本地存储库。那不是它的工作原理-pre-receive钩子应该在您的遥控器上。如果要在推送之前在本地运行某些内容,请使用pre-push挂钩。

the documentation中还列出了其他一些有用的钩子。