我想保护我的 git 存储库,因此只能覆盖非主分支。有没有办法只保护选定的分支?
user1368198
问问题
7225 次
7 回答
7
这是我为自己使用而编写的更新挂钩(复制到挂钩/更新)。默认情况下,此脚本拒绝所有非快进更新,但允许它们用于显式配置的分支。反转它应该很容易,以便除主分支之外的所有分支都允许非快进更新。
#!/bin/sh
#
# A hook script to block non-fast-forward updates for branches that haven't
# been explicitly configured to allow it. Based on update.sample.
# Called by "git receive-pack" with arguments: refname sha1-old sha1-new
#
# Config
# ------
# hooks.branch.<name>.allownonfastforward
# This boolean sets whether non-fast-forward updates will be allowed for
# branch <name>. By default they won't be.
# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"
# --- Safety check
if [ -z "$GIT_DIR" ]; then
echo "Don't run this script from the command line." >&2
echo " (if you want, you could supply GIT_DIR then run" >&2
echo " $0 <ref> <oldrev> <newrev>)" >&2
exit 1
fi
if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
echo "Usage: $0 <ref> <oldrev> <newrev>" >&2
exit 1
fi
# --- Check types
# if $newrev is 0000...0000, it's a commit to delete a ref.
zero="0000000000000000000000000000000000000000"
if [ "$newrev" = "$zero" ]; then
newrev_type=delete
else
newrev_type=$(git cat-file -t $newrev)
fi
case "$refname","$newrev_type" in
refs/tags/*,commit)
# un-annotated tag
;;
refs/tags/*,delete)
# delete tag
;;
refs/tags/*,tag)
# annotated tag
;;
refs/heads/*,commit)
# branch
# git rev-list doesn't print anything on fast-forward updates
if test $(git rev-list "$newrev".."$oldrev"); then
branch=${refname##refs/heads/}
nonfastforwardallowed=$(git config --bool hooks.branch."$branch".allownonfastforward)
if [ "$nonfastforwardallowed" != "true" ]; then
echo "hooks/update: Non-fast-forward updates are not allowed for branch $branch"
exit 1
fi
fi
;;
refs/heads/*,delete)
# delete branch
;;
refs/remotes/*,commit)
# tracking branch
;;
refs/remotes/*,delete)
# delete tracking branch
;;
*)
# Anything else (is there anything else?)
echo "hooks/update: Unknown type of update to ref $refname of type $newrev_type" >&2
exit 1
;;
esac
# --- Finished
exit 0
于 2013-04-02T09:33:39.007 回答
4
您可以使用GitEnterprise设置每个分支权限(管理员),以使用细粒度访问权限阻止非快进推送。
如果git config --system receive.denyNonFastForwards true
您需要阻止所有分支的历史记录更改,它将简单地完成这项工作。
于 2012-07-16T08:08:55.470 回答
1
您可以通过配置防止非快进更新denyNonFastForwards
git config --system receive.denyNonFastForwards true
但它适用于所有分支。有关更多信息,请参阅ProGit
于 2012-07-16T07:44:42.733 回答
0
这个 SO 答案将为您提供所需的内容。只需编辑它以应用于主分支:
#!/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
更新:
这将阻止所有推送到主分支,包括快进。
于 2012-07-16T07:42:38.643 回答
0
如果您被允许修改您的服务器,那么这将在服务器上启用快速转发。
ssh ip 'echo $"[receive]
denyDeletes = false
denyNonFastForwards = false" >> /path/to/repo/config'
#then git push -f origin master
于 2014-04-08T17:08:30.860 回答
0
这是对Tanu Kaskinen脚本的修改,以允许对分支名称进行通配符。我们使用名称以“d/”开头的分支来指定“开发”分支。我想要一种方法来允许这些 d/ 分支的非快进更新:
refs/heads/*,commit)
# branch
# git rev-list doesn't print anything on fast-forward updates
if [[ $(git rev-list "$newrev".."$oldrev") ]]; then
branch=${refname##refs/heads/}
if [[ "$branch" =~ ^d/ ]] ; then
echo "Non-fast-forward update allowed on d/ branch"
nonfastforwardallowed="true";
else
#look for a specific config setting
nonfastforwardallowed=$(git config --bool hooks.branch."$branch".allownonfastforward)
fi
if [ "$nonfastforwardallowed" != "true" ]; then
echo "hooks/update: Non-fast-forward updates are not allowed for branch $branch"
exit 1
fi
fi
于 2020-03-16T20:41:30.847 回答