git - 理解 Git Hook - post-receive hook

标签 git githooks git-post-receive

我编写了简单的 shell 脚本来抛出“成功和失败消息”,并将其放在具有所有适当权限的 .git/hooks/下。我想将此脚本称为后期接收。但是脚本不工作,运行脚本只是工作,但作为接收后 Hook 它不起作用。

是他们遗漏了什么还是我错误地理解了接收后 Hook 。有人可以解释客户端和服务器端的钩子(Hook)以及如何执行它们。

我已经搜索过但无法理解。

最佳答案

启用 post-receive hook 脚本,将文件放在 .git 目录的 hooks 子目录中,该文件同名(没有任何扩展名)并使其可执行:

touch GIT_PATH/hooks/post-receive
chmod u+x GIT_PATH/hooks/post-receive

For more info check this doc: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks


例子
检查这个例子(一个简单的部署)GIT_PATH/hooks/post-receive :
#!/bin/bash
TARGET="/home/webuser/deploy-folder"
GIT_DIR="/home/webuser/www.git"
BRANCH="master"

while read oldrev newrev ref
do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref = refs/heads/$BRANCH ]];
    then
        echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
    else
        echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
done

Source: https://gist.github.com/noelboss/3fe13927025b89757f8fb12e9066f2fa#file-

关于git - 理解 Git Hook - post-receive hook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28106011/

相关文章:

git 错误 : gnutls_handshake() failed on push

git - Windows 的后接收脚本(电子邮件通知)

html - 通过 git post-receive 将 styles.less 替换为 styles.css

git - 接收端不支持推送选项

git - 跳过 Git 提交钩子(Hook)

Git 接收后 Hook 不起作用

Git 钩子(Hook)阻止对某些文件夹的更改

git - 为 git-log 和 git-show 进行不同的 `pretty` 设置

git - 是否可以撤消 GitHub 上的 git push --force?