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

参考文章

相关推荐
jiayong239 小时前
Git 核心概念:Tag 与 Branch 的本质区别
git
Serene_Dream12 小时前
git 合并冲突的分支
git
我是一只puppy13 小时前
使用AI进行代码审查
javascript·人工智能·git·安全·源代码管理
玄同76514 小时前
Git常用命令指南
大数据·git·elasticsearch·gitee·github·团队开发·远程工作
十步杀一人_千里不留行17 小时前
Git提交前ESLint校验实践(Husky + lint-staged)
git·github
hh随便起个名20 小时前
适合小白的git的基础使用方法
git
我会一直在的20 小时前
Devps持续集成
git·ci/cd
CoderJia程序员甲21 小时前
GitHub 热榜项目 - 日榜(2026-02-08)
git·ai·开源·llm·github
Serene_Dream1 天前
git 常用命令
git
jiayong231 天前
Detached HEAD 状态详解
git