防止在master分支中提交 [英] Prevent commits in master branch

查看:113
本文介绍了防止在master分支中提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(为简单起见),我的Git-repo中有一个 master 分支和一个 dev .我想确保 master 分支始终正常工作,所以我所做的所有工作都应该在 dev 分支中.

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

但是,当我使用-no-ff merge合并我的更改时,我倾向于留在 master 分支中,然后继续在其中工作(因为我忘记签出 dev 分支).

However, when I merge my changes in with a --no-ff merge, I tend to stay in the master branch, and just continue working in it (because I forget to checkout my dev branch).

我可以为 master 分支建立一条规则,该规则指出我无法执行提交和快速合并,但只能执行-no-ff 从另一个分支合并?

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

这必须适用于私有托管存储库(ergo不适用于GitHub和BitBucket).

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

推荐答案

是的,有可能.您必须创建预提交钩子,该钩子拒绝对master分支的提交.当您调用 merge 命令时,Git不会调用预提交钩子,因此该钩子将仅拒绝常规提交.

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 merge command, so this hook will be rejecting only regular commits.

  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

  • 使其可执行(在 Windows 上不是必需的):

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

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

    To disable fast-forward merges you must also add following option to your .git/config file:

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

    如果您还希望保护遥控器上的master分支,请检查以下答案:

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

    这篇关于防止在master分支中提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆