防止在 git 存储库中的 master 分支上直接提交并仅接受合并?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7052686/
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
Prevent direct commits on master branch in git repository and accept merges only?
提问by alexbilbie
My git repository has two branches, 'master' and 'dev'.
我的 git 存储库有两个分支,'master' 和 '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'?
是否有可能使用钩子或其他东西来防止在“master”分支上进行正常的直接提交并且只接受从“dev”到“master”的合并?
回答by Ryan Stewart
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.
不是直接的答案:为此考虑使用 repos 而不是分支。想象三个存储库:本地、开发和祝福。本地 = 您自己工作的仓库。Dev = 您将所有提交推送到的存储库以及构建过程正在监视更改的存储库。Blessed = 只有构建过程可以推送到并且你从中拉取的存储库。因此,您提交到本地并将更改推送到开发。自动构建会测试您推送的提交并在成功时将它们推送到祝福状态。然后你(或其他任何人)可以从有福的地方接他们并从那里继续工作。
回答by Fred Foo
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
您可能希望使用一个commit-msg
钩子来检查该词是否merge
出现在临时提交的消息中。就像是
grep -iq merge "" || 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.
检查分行后。您可能想让 RE 比这更严格。当然,这只是一种启发式方法,任何对中央存储库具有写访问权限的人都可以绕过此检查。
回答by thelem
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.
如果你使用 GitHub,他们有一个功能来保护分支。转到存储库的 GitHub 设置,然后分支并查看受保护的分支设置。
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/
见https://help.github.com/articles/defining-the-mergeability-of-pull-requests/
Bitbucket offer a similar feature.
Bitbucket 提供了类似的功能。
回答by Wissam Youssef
回答by Ahmed Younes
回答by Jitendra
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
现在你所有的本地工作保存(通过 add . & commit )到分支,然后通过推送到远程
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.
之后,您可以向 master 发出 pull request 并合并到 master。这是基于 linux (Ubuntu) 系统环境的答案。
If any miss then let me know?
如果有任何错过,请告诉我?