Is it possible to disable do “push force”, which o

2020-06-06 04:19发布

问题:

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 this commit. But if for some branches it is not so critical, then for the trunk "master" is very critical. Is it possible to disable do "push force", which overwrite "master" trunk of repository, and how can I switch off it?

回答1:

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



标签: git