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

参考文章

相关推荐
Dontla4 小时前
Git error: fatal: detected dubious ownership in repository at xxx
git
论迹5 小时前
【Git】-- 多人协作
git·ubuntu
roamingcode6 小时前
从混沌到秩序:Git Diff 结构化报告的 Claude Code Skill 实践
人工智能·git·agent·skill·claude code·领域知识包·ai经验复用
ss2738 小时前
idea中git更新项目:将传入更改合并到当前分支,在传入更改上变基当前分支
java·git·intellij-idea
hopsky8 小时前
好用!Git 同时开发多个分支
git
secondyoung10 小时前
Git使用:rebase用法
c语言·经验分享·git·vscode
萧曵 丶11 小时前
CI/CD 流程
git·ci/cd
import_random11 小时前
[git版本控制]git push(详解)
git
bing_feilong13 小时前
gitee pull 总是需要用户名和密码,该如何设置?
git·gitee
毒鸡蛋13 小时前
GIT报错:Connection to git. closed by remote host.
git