简体   繁体   English

Git-在允许推送到分支之前需要合并来自master的提交

[英]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: 这相当简单,但有一些小缺点:

  • If the same push also updates 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的新更改,因此在这种情况下,您可能会得到错误的否定消息(未合并更改,但推送通过了)。
  • If the last (and only) new thing that happened on 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM