防止在 git 存储库中的 master 分支上直接提交并仅接受合并?

发布于 2024-11-29 10:46:46 字数 168 浏览 2 评论 0原文

我的 git 存储库有两个分支,“master”和“dev”。

提交给“dev”的代码在测试之前会经历一个自动构建过程。然后,通过此操作的代码将合并到“master”分支中。

是否可以使用钩子或其他东西来阻止“master”分支上的正常直接提交并只接受从“dev”到“master”的合并?

My git repository has two branches, 'master' and 'dev'.

Code committed to 'dev' goes through an automated build process before it is tested. Code that passes this is then merged into the 'master' branch.

Is it possible, using hooks or something else, to prevent normal direct commits on the 'master' branch and only accept merges from 'dev' to 'master'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌逼全场 2024-12-06 10:46:46

不是直接的答案:考虑使用存储库而不是分支。想象一下三个存储库:本地、开发和祝福。本地=您自己工作的存储库。 Dev = 您将所有提交推送到的存储库以及您的构建过程正在监视更改的存储库。 Blessed = 只有构建过程才能推送到的存储库以及从中提取的存储库。因此,您提交到本地并将更改推送到开发人员。自动构建会对您推送的提交进行所有测试,并在成功后将它们推向祝福。然后你(或任何其他人)可以从祝福中拿起它们并从那里继续工作。

Not a direct answer: consider using repos instead of branches for this. Imagine three repos: local, dev, and blessed. Local = your own repo where you work. Dev = the repo you push all your commits to and the one that your build process is monitoring for changes. Blessed = the repo that only the build process can push to and which you pull from. Thus you commit into local and push changes to dev. Auto-build does all it's testing of the commits you pushed and on success, pushes them to blessed. Then you (or anyone else) can pick them up from blessed and continue work from there.

抠脚大汉 2024-12-06 10:46:46

如果您使用 GitHub,它们具有保护分支的功能。转到存储库的 GitHub 设置,然后转到分支并查看受保护的分支设置。

您可以选择要保护哪些分支,以及要如何保护每个分支。您可以阻止强制推送,要求从另一个分支合并更改,甚至要求您的自动化测试已通过。

请参阅 https://help.github.com/articles/defining -the-mergeability-of-pull-requests/

Bitbucket 提供类似的功能。

If you're using GitHub, they have a feature to protect branches. Go to the GitHub settings for the repository, then branches and see the protected branches settings.

You can choose which branches you want to protect, and for each branch how you want to protect it. You can just prevent force pushes, require changes to be merged from another branch, or even require that your automated tests have passed.

See https://help.github.com/articles/defining-the-mergeability-of-pull-requests/

Bitbucket offer a similar feature.

娇柔作态 2024-12-06 10:46:46

您可能需要使用 commit-msg 挂钩来检查单词 merge 是否出现在暂定提交的消息中。 一样

grep -iq merge "$1" || exit 1

就像检查分支之后 。您可能想让 RE 比这更严格。当然,这只是一种启发式方法,任何拥有中央存储库写入权限的人都可以绕过此检查。

You may want to use a commit-msg hook that checks whether the word merge occurs in the message for a tentative commit. Something like

grep -iq merge "$1" || exit 1

after a check for the branch. You may want to make the RE stricter than this. This is only a heuristic, of course, and anyone with write access to the central repo can circumvent this check.

耳钉梦 2024-12-06 10:46:46

考虑使用 git 访问控制层,例如 gitolite

Consider using a git access control layer like gitolite

耳根太软 2024-12-06 10:46:46
npm install -D husky

npm i git-branch-is

通过package.json进行配置

    "husky": {
      "hooks": {
        "pre-commit": "git-branch-is -r \"^((?!master).)*$\""
    }
  }

在此处输入图像描述

npm install -D husky

npm i git-branch-is

configure it through package.json

    "husky": {
      "hooks": {
        "pre-commit": "git-branch-is -r \"^((?!master).)*$\""
    }
  }

enter image description here

著墨染雨君画夕 2024-12-06 10:46:46

创建本地分支

 command: git branch <branch name>

转到分支

Command: git checkout <branch name>     

现在,您所有本地工作都保存(通过 add . & commit )到分支中,然后通过
推送到远程,

command : git push origin <branch name>

之后您可以向 master 发出拉取请求并合并到 master。
这是基于linux(Ubuntu)系统环境的回答。

如果有错过的话请告诉我?

Make local branch

 command: git branch <branch name>

Go to branch through

Command: git checkout <branch name>     

Now your all local work save (through add . & commit ) into branch and then push to remote through

command : git push origin <branch name>

after that you can make pull request to master and merge to master.
This is answer based on the linux (Ubuntu) system environment.

If any miss then let me know?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文