How to prevent remote branch deletion in git witho

2020-07-24 06:23发布

问题:

Is there any way to block deletion of remote branches?

I want to block deletion of remote branches but normal flow like code checking and check out should work fine!!

without using gitolite! is it possible ?

please help !

回答1:

Yes, it is possible. Just add a suitable server side git hook.

You probably want to use a pre-receive hook. For details have a look at here or here.

Example:

#create repositories
git init a
git init --bare b

#add the hook in "b"
echo -e '#!/usr/bin/bash\nread old new ref\ntest $new != 0000000000000000000000000000000000000000' >>b/hooks/pre-receive
chmod +x b/hooks/pre-receive

#create a commit in "a"
cd a
echo foo >test
git add .
git commit -m testcommit

#push it to "b"
git push ../b master

#try to delete remote branch
git push ../b :master


回答2:

 refs/heads/*,delete)
       # delete branch
              if [ "$allowdeletebranch" != "true" ]; then
                echo "*** Deleting a branch is not allowed in this repository" >&2
                    exit 1
                fi

adding this in update hook solved my problem Hope this will help someone else too



回答3:

I'm not sure why you're avoiding gitolite (which is sort of the end point of all access control, as it were), but I have a sample pre-receive script here that uses hooks.* git config entries to do some simple access controls. It's not as fancy as gitolite but it does some things I cared about once. :-)



标签: linux git