Git接收后钩子无法检出,致命的:您正在出生的树枝上

问题描述

在服务器上的/home/user/git/domain.com.git/中,我运行git init --bare,然后在post-receive目录和hooks中创建了chmod +x hooks/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

我知道接收后挂接正在运行,因为我的mkdir给我一个目录已经存在的错误,这是可以预期的,并且在使用此设置的其他站点上也没有问题。

我也想使用git push remotename HEAD这个问题,最终我将使用它来简化脚本。

解决方法

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

参考文献