禁止删除 git 中的 master 分支
我正在尝试设置一个 git 挂钩,禁止任何人删除我们存储库的 master
、alpha
和 beta
分支。谁能帮忙解决这个问题吗?我从来没有做过 git hook,所以我不想在没有一点帮助的情况下尝试自己开发自己的 git hook。
I’m trying to setup a git hook that will disallow anyone to delete the master
, alpha
, and beta
branches of our repository. Can anyone help with this? I have never done a git hook, so I don’t want to try my luck in developing my own without a little help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
pre-receive
钩子就可以直接实现。假设您使用的是裸露的中央存储库,请将以下代码放入your-repo.git/hooks/pre-receive
中,并且不要忘记chmod +x your-repo。 git/hooks/预接收
。Straightforward with a
pre-receive
hook. Assuming you're using a bare central repository, place the following code inyour-repo.git/hooks/pre-receive
, and don't forget tochmod +x your-repo.git/hooks/pre-receive
.如果您愿意通过“推送”拒绝所有分支删除,那么您只需在存储库上将配置变量
receive.denyDeletes
设置为true
即可。如果您确实需要更复杂的控制,我建议您查看 git 发行版的
contrib/hooks
文件夹中的update-paranoid
挂钩。它允许您设置每个引用 acl,它可以执行诸如拒绝非快进和拒绝通过推送删除以及一些更复杂的行为之类的操作。update-paranoid
应该可以完成您需要的一切,而无需编写自己的钩子。If you're happy to deny all branch deletes via 'push' then you can just set the config variable
receive.denyDeletes
totrue
on your repository.If you do need more sophisticated control I recommend that you take a look at the
update-paranoid
hook from the git distribution'scontrib/hooks
folder. It allows you to set up per ref acls which can do things like deny non fast-forwards and deny deletes via push as well as some more sophisticated behaviours.update-paranoid
should do everything you need without you having to write your own hook.