Git post-用于推送到部署的接收挂钩 .. 我想检查最后推送的分支

Git post-receive hook for push-to-deploy .. I want to checkout last pushed branch

我能够通过在服务器上创建一个裸存储库并从我的开发人员推送到它来使用 Git 实现推送部署。下面是我的 post-receive,它处理实时站点的更新:

#!/bin/sh

echo
echo "*** Checking out branch [post-receive hook]"
echo

git --work-tree=/var/www/myproject checkout -f master

请注意,它检查 "master" 分支,这在大多数情况下都非常有效。 "master" 是我推送的分支(例如 git push staging master 推送到登台服务器时)。然而,有时我喜欢只推送一个分支而不将我的更改合并到主分支(例如 git push staging new-register-form)。我可以将我的 post-receive 挂钩脚本更改为结帐 "the last pushed branch" 吗?这可能吗?

是的,你可以,像这样:

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    git --work-tree=/var/www/myproject checkout -f $branch
done

取自:here