2

对于主干“master”,我可以对较早的提交执行“git reset hard”,然后执行“git push force”,以便在存储库中重写它。在这次提交之后,我失去了部分开发。但是,如果对于某些分支来说不是那么关键,那么对于主干“主控”来说是非常关键的。是否可以禁用“推力”,它会覆盖存储库的“主”主干,我该如何关闭它?

4

1 回答 1

8

我遇到过的基于每个分支禁用强制推送的唯一方法是使用预接收挂钩,如https://github.com/olshanov/git-hooks/blob/master/pre-receive所示.deny-force-push-to-branches(我没有写这个钩子,但它很有用)。

这部分的关键是

while read oldrev newrev ref ; do
    # old revision is blank - branch creation
    if [ "$oldrev" = "0000000000000000000000000000000000000000" ] || 
         # new revision is blank - branch deletion
         [ "$newrev" = "0000000000000000000000000000000000000000" ] ||
         # branch != master - pass through
         [ "$ref" != "refs/heads/master" ] ;
    then
        # create new or delete old branch
        # or force pushing to non-master branch
        continue;
    fi

    base=$(git merge-base $oldrev $newrev);
    if [ "$base" != "$oldrev" ] ; then
        # non fast forward merge
        echo "Force pushing of $ref is forbidden to master";
        exit 1;
    fi
done
exit 0;

这仍然允许您删除/创建主分支并强制推送到其他分支,但会阻止您强制推送到主分支本身。

可以通过设置全局禁用强制推送

  • 接收.denyNonFastForwards
  • 接收.拒绝删除

但这会为每个分支关闭它们

于 2013-06-12T11:35:37.500 回答