【问题标题】:Git only allow merge from development into masterGit 只允许从开发合并到 master
【发布时间】:2017-09-08 04:22:08
【问题描述】:

我希望能够设置我们的分支,以便合并只能从开发分支进入 master。

我知道这听起来可能很严厉,我应该问自己一个问题,我是否不信任团队中的开发人员...暂时我不信任,因为他们只是开始熟悉 Git。及时我会取消限制,但在那之前这将是有用的。可能吗?

谢谢, 标记。

【问题讨论】:

  • 如果他们搞砸了,你就不能恢复他们的更改吗?
  • 是的,我可以并感谢您的回复,但这并不能解决问题
  • 如果你可以设置一个钩子并完成它,你不想每隔一周恢复一个凌乱的主人。节省时间和精力。
  • 一些 git 托管服务器允许您禁止推送到某些分支,并且必须提交拉取请求。 Bitbucket 至少做到了这一点。也许这将是一个简单的选择?查找类似于“分支权限”的内容。

标签: git merge branch


【解决方案1】:

作为kowsky answered,可以在服务器端预接收挂钩中执行此操作。不过这出人意料地困难,主要是因为 Git 的分支概念有些模糊。

我写了一个 pre-receive 钩子来做这个,除其他外; the code is available here。请参阅merges_from 函数,并注意其上方的 cmets。由于 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,我会在第一次有机会的时候试一试。不幸的是,我每周只有几个小时在这个特定的项目上。我会回复我的成功程度。
【解决方案2】:

它一点也不苛刻,它非常有意义并且可以防止严重事故。

话虽如此:您可以通过git hooks 控制这样的事情。这些是在某些事件上执行的脚本。在您的情况下,像 thisthis 这样的服务器端预接收或客户端预推送挂钩会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2015-05-18
    • 2020-07-16
    • 2017-07-31
    相关资源
    最近更新 更多