【问题标题】:Git post-receive hook fails to checkout, fatal: You are on a branch yet to be bornGit post-receive hook 无法结帐,致命:你在一个尚未出生的分支上
【发布时间】:2020-10-09 20:15:00
【问题描述】:

在我的服务器上,在/home/user/git/domain.com.git/ 中,我运行了git init --bare,然后在hooks 目录和chmod +x hooks/post-receive 中创建了post-receive 文件。我使用ls -la 来验证它是可执行的。

我的post-receive 文件:

mkdir /home/USER/pub/domain.com
GIT_WORK_TREE=/home/USER/pub/domain.com git checkout -f
GIT_WORK_TREE=/home/USER/pub/domain.com git reset --hard

我从本地执行git push remotename branchname 并得到错误:

remote: fatal: You are on a branch yet to be born

它还显示:

To ssh://domain.com/~/git/domain.com.git
   oldcommithash..newhash  branchname -> branchname

我知道 post-receive 挂钩正在运行,因为我的 mkdir 给了我一个目录已存在错误,这是意料之中的,并且在我使用此设置的其他网站上没有问题。

git push remotename HEAD 也有这个问题,我最终想使用它来编写更简单的脚本。

【问题讨论】:

    标签: git remote-server githooks


    【解决方案1】:

    我最新的hooks/post-receive 脚本,它将用最新的推送更新/home/user/PATH

    #!/bin/bash
    
    user=REMOTE_USER_NAME
    path=pub/domain.com
    
    # Loop over all pushed branches
    c=0;
    while read oldrev newrev refname
    do
        # track count of branches
        c=$((c + 1))
        branch=$(git rev-parse --symbolic --abbrev-ref "$refname")
    done
    # If more than one branch was pushed, give an error & return
    if [[ $c -gt 1 ]];then
        echo ""
        echo "Multiple branches were pushed. Cannot update remote site."
        echo ""
        return;
    fi
    # Make the directory. Gives an error if it exists, but who cares?
    mkdir "/home/${user}/${path}"
    
    # Checkout the pushed branch & reset --hard
    GIT_WORK_TREE="/home/${user}/${path}" git checkout -f "${branch}"
    GIT_WORK_TREE="/home/${user}/${path}" git reset --hard
    

    参考文献

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 2018-12-12
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2014-06-02
      • 2013-07-17
      相关资源
      最近更新 更多