【问题标题】:git branch permissionsgit 分支权限
【发布时间】:2012-10-26 05:47:42
【问题描述】:

是否可以使用 git bash 设置分支权限?我希望对 master 分支有更严格的权限,这样有些人可以使用开发分支并提交到它,并且可能不会自己更改 master 分支。

如果可能的话,我会怎么做呢?

【问题讨论】:

标签: git branch


【解决方案1】:

Git 没有特定于分支的权限。您可以将整个存储库 read only 提供给人们,也可以创建一个私有和一个公共存储库,仅将开发分支推送给公众,同时将主分支仅保留在您的私有存储库中。

编辑: 对于特定于分支的权限,您需要像 Gitolite 这样的服务器端授权层——显然,这需要您管理自己的 Git 服务器。

【讨论】:

【解决方案2】:

可能需要这样做的典型场景是将官方(或发布)分支的访问权限限制为团队中的一部分人员。这里一个好的策略可能是有两个 repo——一个是访问控制更严格的主 repo,另一个是团队中每个人都可以访问并用于设置工作分支的 repo。并根据需要从工作分支拉到主仓库。当然,您可以对其进行调整以适应您的团队结构和需求。

这尤其适用于 github 等服务。

【讨论】:

    【解决方案3】:

    bitbucket 支持分支限制。请参阅此处的链接:https://blog.bitbucket.org/2013/09/16/take-control-with-branch-restrictions/

    【讨论】:

      【解决方案4】:

      如果你的开发团队是文明人,只需要友情提醒,你可以使用pre-receive server-side hook拒绝推送:

      #!/bin/bash
      
      # Extract the user email (%ae) from the last commit (author email)
      USER_EMAIL=$(git log -1 --format=format:%ae HEAD)
      
      # Looping through all the pushed branches
      while read oldrev newrev refname
      do
          branch=$(git rev-parse --symbolic --abbrev-ref $refname)
          if [ "master" == "$branch" ] && [ "the_integrator@your_company.com" != $USER_EMAIL ]; then
              echo "Naughty naughty!"
              exit 1 # fail, i.e. reject push
          fi
      done
      

      虽然用户可以轻松伪造他们的 git 电子邮件地址,但我仍然会将挂钩文件本身设为只读。

      参考:

      1. How can I get push user information in server side git hook?
      2. Writing a git post-receive hook to deal with a specific branch

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-06
        • 2019-02-07
        • 1970-01-01
        • 2014-06-27
        • 2010-12-25
        • 2012-10-17
        • 2017-05-04
        • 2014-09-08
        相关资源
        最近更新 更多