我有一个远程服务器,我在上面创建了一个裸 git 存储库。
我想创建一个钩子(Hook),以便给定脚本在任何给定分支上收到的最新代码上运行。
我知道 post-receive
钩子(Hook)可以用于它。但是我想执行以下操作:
- 获取收到代码的分支机构。
- 在临时位置 checkout 代码并对其运行操作。
我该怎么做?
最佳答案
- 对于 1),您有一个读取分支名称的示例(通过标准输入):
"Not able to detect branch from Git post-receive hook " - 对于2),你需要适本地设置GIT_WORK_TREE:
"Managing website using git + post-receive hook: error pushing changes "
查看“Git post-receive for multiple remote branches and work-trees”中结合的两种技术:
#!/bin/bash
while read oldrev newrev ref
do
branch=`echo $ref | cut -d/ -f3`
if [ "master" == "$branch" ]; then
git --work-tree=/path/under/root/dir/live-site/ checkout -f $branch
echo 'Changes pushed live.'
fi
if [ "dev" == "$branch" ]; then
git --work-tree=/path/under/root/dir/dev-site/ checkout -f $branch
echo 'Changes pushed to dev.'
fi
done
关于git - 如何在git中的接收后 Hook 中处理分支上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11322356/