git - 如何在git中的接收后 Hook 中处理分支上的文件

标签 git githooks

我有一个远程服务器,我在上面创建了一个裸 git 存储库。

我想创建一个钩子(Hook),以便给定脚本在任何给定分支上收到的最新代码上运行。

我知道 post-receive 钩子(Hook)可以用于它。但是我想执行以下操作:

  1. 获取收到代码的分支机构。
  2. 在临时位置 checkout 代码并对其运行操作。

我该怎么做?

最佳答案

查看“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/

相关文章:

git - VS 2017 团队资源管理器 - 没有可用的存储库

Git 钩子(Hook)发送有关 repo 更改的电子邮件通知

ruby - 如何在我的 git hook 目录之外进行 cd

git - 使用ssh克隆git仓库时出错

git - 多个(超过 1 个)私有(private) Github 存储库连接到 cpanel - 无法创建第二个 cpanel 存储库

没有自动提交的 Git merge

git - 是否可以在 gitlab 8 中使用自定义 Hook 进行项目提交?

git - 对于每个提交,在单独的 repo 或分支中创建一个等效的编译提交

python - 如何调试/正确设置 git-multimail

git - merge 分支后推送时无法解析引用 refs/refs/remotes/origin/branch