问题描述
在服务器上的/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
参考文献
- https://stackoverflow.com/a/13057643/802469给了我这个解决方案的while循环
- 几乎重复的git - remote: fatal: you are on a branch yet to be born,using post-receive hook,建议指定分支的名称,但这不是我想要的