代码之家  ›  专栏  ›  技术社区  ›  Reed

Git post receive hook无法签出,致命错误:您在一个尚未出生的分支上

  •  0
  • Reed  · 技术社区  · 5 年前

    /home/user/git/domain.com.git/ 我跑了 git init --bare ,然后创建 post-receive 归档 hooks 总监& chmod +x hooks/post-receive ls -la 验证它是可执行的。

    接收后 文件:

    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
    

    mkdir 给我一个目录已经存在的错误,这是意料之中的,并导致我的其他网站使用此设置没有问题。

    git push remotename HEAD 最终,我想用它来简化脚本。

    1 回复  |  直到 5 年前
        1
  •  0
  •   Reed    5 年前

    我最新的 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