git hooks配置

背景

用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的同步

参考文章

相关推荐
北极糊的狐5 小时前
钉钉小程序 Git 版本管理完整流程
git·小程序·钉钉
Fzuim14 小时前
当 AI 也成为提交者:ThinkFlow 的 Git 提交规范,是怎么定的
git·agent·thinkflow
CodexDave1 天前
数据库连接池耗尽:排查顺序与三层兜底
服务器·前端·数据库·git·云原生·容器·kubernetes
乐观的Terry1 天前
5、发布系统-Git 集成
大数据·git·elasticsearch
不怕犯错,就怕不做1 天前
GIT的简单打patch应用format-patch and git am
linux·git·全文检索
Byron Loong1 天前
【Git】如何检查 Ubuntu 系统上 gitLab 是否开启
git·ubuntu·gitlab
lar_slw1 天前
git删除上一次提交
git
leoZ2312 天前
Git 集成实战完全指南(四):Git 冲突解决
大数据·git·elasticsearch
枫荷2 天前
Git LFS 大文件优化说明
git
techdashen2 天前
不用再反复 stash:用 Git Worktree 同时开发多个分支
大数据·git·elasticsearch