如何限制对 git 上的 master 分支的访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38864405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:28:20  来源:igfitidea点击:

How to restrict access to master branch on git

gitgitlabbitbucketgit-branch

提问by Harshit Agrawal

I have a single repo in which I have 2 user groups:

我有一个存储库,其中有 2 个用户组:

  • Admins
  • HTML/UI Developers
  • 管理员
  • HTML/UI 开发人员

I do not wish to give read/write access of my masterbranch to my HTML developers as they do not need to work on it and should not be misusing the core code.

我不希望将我的master分支的读/写访问权限授予我的 HTML 开发人员,因为他们不需要处理它并且不应该滥用核心代码。

Though they need to work on their own branch ui-developers. How do I do this on git?

尽管他们需要在自己的分支上工作ui-developers。我如何在 git 上做到这一点?

P.S.: I am using BitBucket

PS:我正在使用 BitBucket

UPDATE:

更新:

I had posted this question when I was extremely new to coding and git. After 2 years of experience, I now know that git doesn't allow to restrict READ access for any particular branch. But services like Bitbucket, Gitlab, Github allow you to put write and merge restrictions to branches.

当我对编码和 git 非常陌生时,我发布了这个问题。经过 2 年的经验,我现在知道 git 不允许限制任何特定分支的 READ 访问。但是像 Bitbucket、Gitlab、Github 这样的服务允许你对分支设置写入和合并限制。

Also, I shifted to Gitlab 6 months back :)

另外,我在 6 个月前转向 Gitlab :)

回答by 1615903

Git itself does not have such feature, but many hosting providers do. This is generally known as branch protection. There is no way to prevent read access as far as I know.

Git 本身没有这样的功能,但许多托管服务提供商都有。这通常称为分支保护。据我所知,没有办法阻止读取访问。

Using BitBucket

使用 BitBucket

BitBucket allows a lot of customization for the actions to prevent by branch protection. To protect a branch:

BitBucket 允许对通过分支保护阻止的操作进行大量自定义。保护分支:

  1. Go to a repository in a project.

  2. Choose Settings > Branch permissions.

  3. Click Add permission.

  4. In the Branches field, select either Branch name, Branch pattern, or Branching model.

    • Branch name - select an existing branch by name.

    • Branch pattern - specify a branch using branch pattern syntax for matching branch names.

    • Branching model - select the branch type to restrict access to.

  5. Select the type of actions you want to prevent.

    • Branch deletion - prevents branch and tag deletion.

    • Rewriting history - prevents history rewrites on the specified branch(es) - for example by a force push or rebase.

    • Changes without a pull request - prevents pushing changes directly to the specified branch(es); changes are allowed only with a pull request.

    • All modifications - prevents pushes to the specified branch(es) and restricts creating new branches matching the specified branch(es) or pattern.

  6. Optional: Add exemptions for any of the selected restrictions. Adding a user or group as an exemption means that it will not apply to them. This is not required; not adding any exemptions means the restriction will apply to everyone.

  7. Click Create to finish.

  1. 转到项目中的存储库。

  2. 选择设置 > 分支权限。

  3. 单击添加权限。

  4. 在分支字段中,选择分支名称、分支模式或分支模型。

    • 分支名称 - 按名称选择现有分支。

    • 分支模式 - 使用分支模式语法指定分支以匹配分支名称。

    • 分支模型 - 选择要限制访问的分支类型。

  5. 选择要阻止的操作类型。

    • 分支删除 - 防止分支和标签删除。

    • 重写历史记录 - 防止在指定分支上重写历史记录 - 例如通过强制推送或变基。

    • 没有拉取请求的更改 - 防止将更改直接推送到指定的分支;只有拉取请求才允许更改。

    • 所有修改 - 防止推送到指定分支并限制创建与指定分支或模式匹配的新分支。

  6. 可选:为任何选定的限制添加豁免。添加用户或组作为豁免意味着它不适用于他们。这不是必需的;不添加任何豁免意味着限制将适用于所有人。

  7. 单击创建完成。

Source

来源

Using GitHub

使用 GitHub

Protected branch in GitHub:

GitHub 中的受保护分支:

  • Can't be force pushed

  • Can't be deleted

  • Can't have changes merged into them until required status checks pass

  • 不能强推

  • 无法删除

  • 在所需的状态检查通过之前不能将更改合并到其中

To protect a branch:

保护分支:

  1. On GitHub, navigate to the main page of the repository.

  2. Under your repository name, click Settings.

  3. In the left menu, click Branches.

  4. Under Protected Branches, select the branch you want to mark protected using the drop-down menu.

  5. Select Protect this branch.

  6. Click Save changes.

  1. 在 GitHub 上,导航到存储库的主页。

  2. 在您的存储库名称下,单击设置。

  3. 在左侧菜单中,单击分支。

  4. 在受保护的分支下,使用下拉菜单选择要标记为受保护的分支。

  5. 选择保护此分支。

  6. 单击保存更改。

Source

来源

Using GitLab

使用 GitLab

In GitLab, protecting a branch does the following:

在 GitLab 中,保护分支执行以下操作:

  • it prevents its creation, if not already created, from everybody except users with Master permission
  • it prevents pushes from everybody except users with Master permission
  • it prevents anyone from force pushing to the branch
  • it prevents anyone from deleting the branch
  • 它会阻止除具有 Master 权限的用户之外的所有人创建(如果尚未创建)
  • 它可以防止除拥有 Master 权限的用户之外的所有人推送
  • 它可以防止任何人强行推向分支
  • 它可以防止任何人删除分支

To protect the branch:

保护分支:

  1. Navigate to the main page of the project.

  2. In the upper right corner, click the settings wheel and select Protected branches.

  3. From the Branch dropdown menu, select the branch you want to protect and click Protect.

  4. Once done, the protected branch will appear in the "Already protected" list.

  1. 导航到项目的主页。

  2. 在右上角,单击设置轮并选择受保护的分支。

  3. 从分支下拉菜单中,选择要保护的分支,然后单击保护。

  4. 完成后,受保护的分支将出现在“已受保护”列表中。

You can then allow some access to users with developer rights by checking "Developers can merge" or "Developers can push"

然后,您可以通过选中“开发人员可以合并”或“开发人员可以推送”来允许具有开发人员权限的用户进行某些访问

Source

来源

回答by Mahmut Acar

Addition to @1615903's answer, you can protect a branch in Azure DevOps like below:

除了@1615903 的回答之外,您还可以保护 Azure DevOps 中的分支,如下所示:

Using Azure DevOps

使用 Azure DevOps

In Azure DevOps, protecting a branch with security permissions and policies which allow you can do the following:

在 Azure DevOps 中,使用允许您执行以下操作的安全权限和策略来保护分支:

  • Add or remove users and groups to a specific branch.
  • Declare a minimum number of reviewers for pull requests.
  • Automatically include code reviewers
  • Automatically check linked work items is required or optional
  • Check for comment resolution
  • Enforce a merge strategy(No-fast-forward or squash)
  • Build validation
  • 向特定分支添加或删除用户和组。
  • 为拉取请求声明最少数量的审阅者。
  • 自动包括代码员
  • 自动检查链接的工作项是必需的还是可选的
  • 检查评论分辨率
  • 强制执行合并策略(无快进或挤压)
  • 构建验证

and more detailed informationon the docs page.

以及文档页面上的更多详细信息

To protect the branch:

保护分支:

  1. Select Repos > Branchesin the project's main page to open the Branches.
  2. Locate your branch on the page in which all branches are listed.
  3. Select the ...button. Then select Branch policiesor Branch securityfrom the context menu.
  4. Once you have done, select Save changesto apply your new policy configuration.
  1. 在项目主页中选择Repos > Branches打开 Branches
  2. 在列出所有分支的页面上找到您的分支。
  3. 选择...按钮。然后 从上下文菜单中选择分支策略分支安全
  4. 完成后,选择保存更改以应用新的策略配置。

回答by Jose F. Velez

In Git itself you can use the "pre-receive" hook in the server to protect for writing in a branch to some user.

在 Git 本身中,您可以使用服务器中的“pre-receive”挂钩来保护某个用户在分支中的写入。

Using this hook you can apply your restriction policies for each file in the push.

使用这个钩子,你可以为推送中的每个文件应用你的限制策略。

Hereyou can find an example.

在这里你可以找到一个例子。