是否可以禁用“push force”,它会覆盖存储库的“master”主干?

时间:2013-06-12 10:42:06

标签: git

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

1 个答案:

答案 0 :(得分:8)

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

这个关键部分是

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;

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

可以通过设置

来全局禁用强制推送
  • receive.denyNonFastForwards
  • receive.denyDeletes

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