How can I prevent non-fastforward pushes to selected branch(es) in git?

前端 未结 7 1259
醉话见心
醉话见心 2020-12-10 06:46

I would like to protect my git repository so only non master branches can be overwritten. Is there a way to protect only selected branches?

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 07:22

    This SO answer will give you what you're looking for. Just edit it to apply to the master branch instead:

    #!/bin/sh
    # lock the master branch for pushing
    refname="$1"
    
    if [ "$refname" = "refs/heads/master" ]
    then
        echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        echo "You cannot push to the master branch."
        echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
        exit 1
    fi
    exit 0
    

    Update:
    This will prevent all pushes to the master branch, including fast-forward.

提交回复
热议问题