防止在 git 存储库中的 master 分支上直接提交并仅接受合并?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不是直接的答案:考虑使用存储库而不是分支。想象一下三个存储库:本地、开发和祝福。本地=您自己工作的存储库。 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.
如果您使用 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.
您可能需要使用
commit-msg
挂钩来检查单词merge
是否出现在暂定提交的消息中。 一样就像检查分支之后 。您可能想让 RE 比这更严格。当然,这只是一种启发式方法,任何拥有中央存储库写入权限的人都可以绕过此检查。
You may want to use a
commit-msg
hook that checks whether the wordmerge
occurs in the message for a tentative commit. Something likeafter 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.
考虑使用 git 访问控制层,例如 gitolite
Consider using a git access control layer like gitolite
通过package.json进行配置
configure it through package.json
创建本地分支
转到分支
现在,您所有本地工作都保存(通过 add . & commit )到分支中,然后通过
推送到远程,
之后您可以向 master 发出拉取请求并合并到 master。
这是基于linux(Ubuntu)系统环境的回答。
如果有错过的话请告诉我?
Make local branch
Go to branch through
Now your all local work save (through add . & commit ) into branch and then push to remote through
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?