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

参考文章

相关推荐
ruanCat31 分钟前
前端工程化工具链从零配置:simple-git-hooks + lint-staged + commitlint
前端·git·代码规范
木子小喵2 小时前
Git的使用介绍!超通俗!
git
coderYYY2 小时前
git push报错Authentication failed for ‘xxx’也不会弹要求输入用户名密码的最终解决方法
前端·git·gitee·github
@PHARAOH3 小时前
WHAT - git worktree 开发的并发模型
大数据·git·elasticsearch
苦瓜小生4 小时前
【Git】| 将拉下来的代码上传到自己的 Gitee 仓库(手把手教学)
git·gitee
WKP94184 小时前
git的merge和rebase操作
git
___波子 Pro Max.7 小时前
Git 分支切换
git
win x7 小时前
一篇搞懂Git基础操作
linux·git
一个有温度的技术博主8 小时前
网安实验系列五:.git源代码泄露
git
吴声子夜歌8 小时前
TypeScript——索引类型、映射对象类型、条件类型
git·ubuntu·typescript