Git只允许从开发分支合并到主分支。

17

我希望能够设置我们的分支,使得合并只能从开发分支到主分支。

我知道这听起来可能有些严苛,我应该问问自己,是否不信任团队中的开发人员…… 目前我还是不信任他们,因为他们刚开始熟悉Git。随着时间的推移,我会取消这个限制,但在此之前,这将是很有用的。这种设置可行吗?

谢谢, Mark。


如果他们搞砸了,你不能只是还原他们的更改吗? - vallentin
1
是的,我可以,谢谢您的回复,但这并没有解决问题。 - Mark Gargan
3
如果您能设置钩子并完成它,就不必每隔一周就还原混乱的主分支,这样可以节省时间和精力。注意不要改变原意。 - kowsky
1
一些Git托管服务器允许您禁止推送到某些分支,并且必须提交拉取请求。至少Bitbucket可以这样做。也许这将是一个简单的选项?寻找类似于“分支权限”的东西。 - Lasse V. Karlsen
在这里开始了一次讨论:https://github.com/orgs/community/discussions/56942 - Lenny4
2个回答

8
kowsky answered所述,可以在服务器端使用预接收钩子来实现此操作。但是这实际上相当困难,主要是因为Git对分支的概念有点含糊。
我编写了一个预接收钩子,其中包括此功能; 代码在这里可用。请参见merges_from函数,并注意其上方的注释。由于链接在StackOverflow中有些棘手,因此我也会在此处包含实际函数。(请注意,此代码旨在与古老版本的Git一起使用,因此不使用像git for-each-ref --merged这样的现代功能。)
# $1 is a merge commit.  Find all its parents, except the first
# (which is "on" the branch being merged-into and hence not relevant).
# For each remaining parent, find which branch(es) contain them.
# If those branch heads *are* them, consider that the "source" of the merge.
#
# Note that this means if branches A and B both name commit 1234567,
# and you merge commit 1234567 into branch master, this considers
# the merge (with its one parent) to merge both branches A and B.
# That may not be ideal, but it is what we get...
merges_from()
{
    local branches b src

    set -- $($GIT rev-list --parents --no-walk $1)
    shift 2
    for i do
        # git branch --contains may print something like
        #    * (no branch)
        # or
        #    * (detached from blah)
        # if HEAD is detached.  This should not happen in
        # a bare repo, but if it does, we'll add HEAD to
        # our list.
        branches=$($GIT branch --merged $i |
            sed -e 's/\* (.*)/HEAD/' -e 's/^[* ]//')
        set -- $branches
        src=""
        for b do
            if [ $($GIT rev-parse $b) = $i ]; then
                src="${src}${src:+ }$b"
            fi
            [ "$src" == "" ] && src="-"
        done
        echo $src
    done
}

非常感谢Torek,我会尽快尝试这个方法。不幸的是,我每周只有几个小时可以用在这个特定的项目上。我会回复您我的进展情况。 - Mark Gargan
@MarkGargan 有什么消息吗? - Michael

0

这并不是严苛的规定,它非常合理,可以避免严重的事故。

话虽如此:你可以通过git hooks来控制这样的事情。这些脚本会在特定事件发生时执行。在你的情况下,一个服务器端的pre-receive或客户端的pre-push hook,例如thisthis,将会有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接