Git:防止在主分支中提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40462111/
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:37:02  来源:igfitidea点击:

Git: Prevent commits in master branch

gitmergefast-forward

提问by Rasmus B?kgaard

(For simplicity) I have a masterbranch and a devin my Git-repo. I want to ensure the masterbranch is always working, so all work I do should be in the devbranch.

(为简单起见)我的 Git 存储库中有一个master分支和一个dev。我想确保master分支一直在工作,所以我所做的所有工作都应该在dev分支中进行。

However, when I merge my changes in with a --no-ffmerge, I tend to stay in the masterbranch, and just continue working in it (because I forget to checkout my devbranch).

但是,当我将更改与--no-ff合并合并时,我倾向于留在master分支中,并继续在其中工作(因为我忘记检查dev分支)。

Can I put up a rule for the masterbranch, which states I can't do commits, and fast-forward merges, but only --no-ffmerges from another branch?

我可以为master分支制定一个规则,规定我不能提交和快进合并,但只能--no-ff从另一个分支合并吗?

This must work for private hosted repositories (ergo not GitHub and BitBucket).

这必须适用于私有托管存储库(因此不适用于 GitHub 和 BitBucket)。

回答by qzb

Yes, it is possible. You must create pre-commit hook which rejects commits to master branch. Git doesn't call pre-commit hook when you call mergecommand, so this hook will be rejecting only regular commits.

对的,这是可能的。您必须创建拒绝提交到 master 分支的预提交钩子。当你调用merge命令时,Git 不会调用 pre-commit 钩子,所以这个钩子只会拒绝常规提交。

  1. Go to your repository.
  2. Create file .git/hooks/pre-commitwith following content:

    #!/bin/sh
    
    branch="$(git rev-parse --abbrev-ref HEAD)"
    
    if [ "$branch" = "master" ]; then
      echo "You can't commit directly to master branch"
      exit 1
    fi
    
  3. Make it executable (not required on Windows):

    $ chmod +x .git/hooks/pre-commit
    
  1. 转到您的存储库。
  2. 使用以下内容创建文件.git/hooks/pre-commit

    #!/bin/sh
    
    branch="$(git rev-parse --abbrev-ref HEAD)"
    
    if [ "$branch" = "master" ]; then
      echo "You can't commit directly to master branch"
      exit 1
    fi
    
  3. 使其可执行(在Windows 上不需要):

    $ chmod +x .git/hooks/pre-commit
    

To disable fast-forwardmerges you must also add following option to your .git/configfile:

要禁用快进合并,您还必须在.git/config文件中添加以下选项:

[branch "master"]
    mergeoptions = --no-ff

If you want also protect master branch on your remote, check this answer: How to restrict access to master branch on git

如果您还想保护远程上的 master 分支,请查看此答案:如何限制对 git 上的 master 分支的访问

回答by Michel Samia

It may make sense to install it globally via

通过全局安装它可能是有意义的

git config --global core.hooksPath ~/githooks

and moving that pre-commitfile into that directory

并将该pre-commit文件移动到该目录中

回答by JGC

You can use the pre-commitutility to do this. It has a built in no-commit-to-branchhook that can be used to prevent commits to one or more branches.

您可以使用预提交实用程序来执行此操作。它有一个内置的no-commit-to-branch钩子,可用于防止提交到一个或多个分支。

Setup

设置

The basic setup process is:

基本的设置过程是:

  • Install using pip or brew (instructions at https://pre-commit.com/#install)
  • Create a .pre-commit-config.yamlfile in the root of your project (see below for a first draft)
  • Install the hooks into your git config by running pre-commit install.
  • 使用 pip 或 brew 安装(https://pre-commit.com/#install 上的说明)
  • .pre-commit-config.yaml在项目的根目录中创建一个文件(请参阅下面的初稿)
  • 通过运行将钩子安装到您的 git 配置中pre-commit install

Basic config for protecting branches

保护分支的基本配置

Here is a basic config that includes just the no-commit-to-branchhook:

这是一个仅包含no-commit-to-branch钩子的基本配置:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v2.2.5
  hooks:
    - id: no-commit-to-branch
      args: ['--branch', 'master']

If you want to protect multiple branches you can use include multiple --branchargs in the argument list:

如果你想保护多个分支,你可以--branch在参数列表中使用包含多个参数:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
  rev: v2.2.5
  hooks:
    - id: no-commit-to-branch
      args: ['--branch', 'master', '--branch', 'staging']

Isn't this all overkill?

这不是矫枉过正吗?

Pre-commit has many other built-in hooks, and a large collection of community-built hooksthat will transform the way you clean-up and validate your commits. The reason I mention this is because, while this tool may be overkill for just preventing commits to a protected branch, it has many other features that make it a compelling and simple addition to any git project.

Pre-commit 有许多其他内置挂钩,以及大量社区构建的挂钩,它们将改变您清理和验证提交的方式。我提到这一点的原因是,虽然这个工具对于阻止提交到受保护的分支可能有点过分,但它还有许多其他功能,使其成为任何 git 项目的引人注目且简单的补充。