功能:限制某个分支push的功能
git钩子说明详见:自定义 Git - Git 钩子
1、在客户端本地 .git/hooks/ 目录下新建 commit-msg 文件,权限为755
cd .git/hooks/
cat > commit-msg << END
#!/bin/sh
# 当前分支名称
currentBranch=`git rev-parse --abbrev-ref HEAD`
#如果当前分支为dev,就退出
if [ $currentBranch == "dev" ] ;then
exit 1
fi
END
#授权
chmod 755 commit-msg
备注:
commit-msg 钩子作用:该钩子脚本以非零值退出,Git 将放弃提交
2、扩展知识
需求:只有是自己的分支,才能提交;如果不是自己的分支,想强制提交需要在提交信息中,需要提交信息的第一行为"force commit"
cat > commit-msg << END
#!/bin/sh
# 使用说明
# 1.只有是自己的分支,才能提交,可以在myBranchs中设置,例子:myBranchs=("feature/mbg_dev" "dev")
# 2.如果不是自己的分支,想强制提交需要在提交信息中,需要提交信息的第一行为"force commit"
# 3.myBranchs中的分支名称支持正则表达式,例子:myBranchs=("^feature/mbg_")
# 自己的分支(数组)
myBranchs=("^feature/mbg_")
# 当前分支名称
currentBranch=`git rev-parse --abbrev-ref HEAD`
for i in ${myBranchs[@]}
do
[[ $currentBranch =~ $i ]] && exit 0
done
# 提交信息第一行
commitCntHead=`head -n 1 "$1"`
if [[ $commitCntHead = "force commit" ]]
then
exit 0
else
echo "no authority commit to branch [ " $currentBranch "]"
exit 1
fi
END
备注:
在git 2.9以上版本可以设置全局变量,低版本只能将文件放到项目中的.git/hooks/文件夹中,使用命令: git config --global core.hooksPath /d/git/hooks
,其中/d/git/hooks
代表 D:\git\hooks
。