[英]Git - require merging commits from master before allowing push to branches
We'd like to prevent lengthy merge conflicts between development branches by requiring these branches to pull/merge from the master branch before allowing a push to the development branch (if there are any changes to the master branch). 我们希望通过允许这些分支从主分支中拉出/合并,然后再允许推送到开发分支(如果对主分支进行任何更改)来防止开发分支之间的长时间合并冲突。 Is this possible to enforce?
可以强制执行吗?
Yes, depending on how your central repository is hosted. 是的,取决于中央存储库的托管方式。 If it's hosted at something like GitHub or GitLab.com, this will probably not be possible because these types of providers don't allow you to provide your own code to check pushes (you can configure some restrictions via their web interface but I don't think what you want is possible there).
如果将其托管在GitHub或GitLab.com之类的网站上,则可能无法实现,因为这些类型的提供程序不允许您提供自己的代码来检查推送(您可以通过其网络界面配置一些限制,但我不能)认为您想要的东西在那里可能)。
For a self-hosted repository, you can do this in an update
hook (see the documentation for hooks ). 对于自托管的存储库,您可以在
update
挂钩中执行此操作(请参阅有关挂钩的文档 )。 Here's an example that I haven't actually tested: 这是我尚未实际测试过的示例:
#!/bin/sh
if [ "$1" = "refs/heads/development" -a "$(git rev-list $3..master)" ]; then
echo "ERROR: master is not merged into development, please try again" >&2
exit 1
fi
This essentially checks that there's "nothing left" if you subtract the set of commits from the new state of development
from the set of commits on master
. 如果从
master
上的提交集中减去新的development
状态中的提交集,这实际上将检查是否“一无所有”。 If you have merged, development
will have all of master
's commits so the result will be the empty set. 如果已合并,则
development
将具有master
的所有提交,因此结果将为空集。
This is fairly simple but has a few minor drawbacks: 这相当简单,但有一些小缺点:
master
, the check in this hook may not be aware of the new changes to master
yet, so in that case you might get a false negative (unmerged changes but push goes through). master
,则此挂钩中的检查可能尚未意识到对master
的新更改,因此在这种情况下,您可能会得到错误的否定消息(未合并更改,但推送通过了)。 master
was a merge from development
, the hook will give a false positive of sorts because master
will have that merge commit while development
won't. master
上发生的最后一个(也是唯一的)新事件是来自development
的合并,则该钩子会给出某种错误的肯定,因为master
会具有合并提交,而development
则不会。 Of course we know that that's an immaterial difference between the two branches, but the simple set arithmetic in the hook doesn't understand these distinctions.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.