有没有办法配置 git 存储库以拒绝“git push --force”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1754491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to configure git repository to reject 'git push --force'?
提问by Czlowiekwidmo
I was wondering is there a way to prevent 'git push --force
' on a repository (only on master branch)?
我想知道有没有办法防止 ' git push --force
' 在存储库上(仅在 master 分支上)?
Assume I have remote git repository and do:
假设我有远程 git 存储库并执行以下操作:
- '
git push
' to 'master'. It works. - '
git push --force
' to 'branch-1'. It works. - '
git push --force
' to 'master'. It is rejected.
- '
git push
' 到 '主人'。有用。 - '
git push --force
' 到 'branch-1'。有用。 - '
git push --force
' 到 '主人'。它被拒绝。
Is it even possible?
甚至有可能吗?
Thanks for any answers and suggestions.
感谢您提供任何答案和建议。
BR, Dawid.
BR,大卫。
回答by CB Bailey
Setting the configuration variables:
设置配置变量:
receive.denyNonFastForwards
receive.denyDeletes
will prevent any 'forced' pushes from working across all branches.
将防止任何“强制”推送在所有分支上工作。
If you want finer pre-branch control then you will have to use a 'hook' on the remote repository, probably the 'update' hook.
如果您想要更好的预分支控制,那么您将不得不在远程存储库上使用“钩子”,可能是“更新”钩子。
There is a sample update hook called 'update-paranoid' that probably does what you need (and more) in the git distribution in the 'contrib' folder.
有一个名为“update-paranoid”的示例更新挂钩,它可能会在“contrib”文件夹中的 git 发行版中满足您的需要(以及更多)。
回答by hakeris1010
Github has already introduced the concept of protected branches!
Github 已经引入了受保护分支的概念!
It can be found under Settings -> Branches -> Protected Branches
. Feature is now available for all users - not only enterprise!
它可以在 下找到Settings -> Branches -> Protected Branches
。该功能现在可供所有用户使用 - 不仅仅是企业!
This "protection" can be enabled for any branch, and for any user, including admins.
可以为任何分支机构和任何用户(包括管理员)启用此“保护”。
More details here - https://help.github.com/articles/defining-the-mergeability-of-pull-requests/
更多细节在这里 - https://help.github.com/articles/defining-the-mergeability-of-pull-requests/
So no more hooks and arbitrary code is needed.
所以不需要更多的钩子和任意代码。
回答by Thomas Koch
I wrote this quick update hook to prevent non-fast-forward updates (pushes) on the "dev" branch in a repository:
我编写了这个快速更新挂钩来防止存储库中“dev”分支上的非快进更新(推送):
#!/bin/sh
REFNAME=
OLDSHA=
NEWSHA=
if [ "refs/heads/dev" != $REFNAME ]; then
exit 0
fi
MERGEBASE=$(git merge-base $OLDSHA $NEWSHA)
if [ $OLDSHA = $MERGEBASE ]; then
exit 0
fi
echo "Not a fast-forward on branch dev"
exit 1