背景
用cocoapods管理的iOS项目中需要限制 release 分支直接push
经过一番Google,找到了git hooks限制的方法,但是看了很多文章,发现废话一堆,不能快速的解决我的问题
在这里记录一下
第一步,进入你项目的.git/hooks路径下
可以看到很多sample后缀文件,根据文件名,你可以猜到是控制git操作哪一步的,例如我要控制push,那么我就是修改pre-push那个文件

第二步修改pre-push
- 用编辑器打开,编辑这个文件

- cursour给写的
shell
#!/bin/sh
remote="$1"
url="$2"
# Block pushing directly to release branches (e.g., release or release/*)
block_pattern='^refs/heads/release($|/)'
while read local_ref local_sha remote_ref remote_sha
do
if echo "$remote_ref" | grep -Eq "$block_pattern"; then
echo >&2 "检测到目标分支为受保护分支:$remote_ref"
echo >&2 "禁止直接 push 到 release 分支。请通过 Pull Request 或受保护流程合并。"
exit 1
fi
done
exit 0
- 文件编辑好后,把
.sample
去掉,就会变成一个shell程序
第三步同步hooks
因为hooks是放在.git下面的,但是.git不能上传到远端,想要同步给其他人,还需要做以下操作
- 复制编辑好的
hooks
文件夹到项目的根目录,文件夹名称为.hooks
- 在podfile中随便找个地方写上这样一段命令
system("git config core.hooksPath .hooks")
,在执行pod更新的时候,就会自动配置git config - 以上就完成了git hooks的同步
