Is it possible to disable do “push force”, which overwrite “master” trunk of repository?

前端 未结 1 2075
心在旅途
心在旅途 2021-02-11 03:35

For the trunk \"master\" I can do \"git reset hard\" to an earlier commit and then \"git push force\", to rewrite it in the repository. And I lose part of the development, after

1条回答
  •  心在旅途
    2021-02-11 04:23

    The only way to disable force push on a per branch basis that I've ever come across is with a pre-receive hook as shown at https://github.com/olshanov/git-hooks/blob/master/pre-receive.deny-force-push-to-branches (I didn't write this hook but it is useful).

    The key section of this is

    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;
    

    This will still allow you to delete / create master branch and force push to other branches but will prevent you from force pushing to the master branch itself.

    It is possible to disable force push globally by setting

    • receive.denyNonFastForwards
    • receive.denyDeletes

    but this turns them off for every branch

    0 讨论(0)
提交回复
热议问题