惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
S
Schneier on Security
罗磊的独立博客
有赞技术团队
有赞技术团队
U
Unit 42
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
F
Full Disclosure
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog
爱范儿
爱范儿
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
B
Blog
量子位
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
W
WeLiveSecurity
Recent Announcements
Recent Announcements
G
GRAHAM CLULEY
大猫的无限游戏
大猫的无限游戏
F
Fortinet All Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
Spread Privacy
Spread Privacy
Cisco Talos Blog
Cisco Talos Blog
The Cloudflare Blog
D
Docker
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
Recorded Future
Recorded Future
H
Help Net Security
L
LINUX DO - 热门话题
The Hacker News
The Hacker News
博客园 - 叶小钗
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Threat Research - Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
MongoDB | Blog
MongoDB | Blog
Forbes - Security
Forbes - Security
Vercel News
Vercel News
G
Google Developers Blog
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Cyberwarzone
Cyberwarzone
T
Tor Project blog
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed

技术小黑屋

Vibe Coding 的安全风险与应对策略 - 技术小黑屋 Vibe Coding 最佳实践:10 个让开发效率提升 10 倍的关键技巧 一看就会,为 AI 编程 Agent 撸一个 MCP 服务 同样是 Sonnet 4.5,为何 CLI 工具差距这么大 定位 Android 权限声明来源 - 技术小黑屋 Android 升级 targetSDK 35 解决 namespace 问题 解决 Android Studio 关闭后终端 flutter run 进程自动结束的问题 使用 grep 查找关键字并显示上下文行 - 技术小黑屋 Android 开发中的三个常见构建错误及解决方案 - 技术小黑屋 幂等性的劣化:从数学确定性到 AI 不确定性 - 技术小黑屋 Could not create task ':generateDebugRFile' 问题小记 Android 模拟器实现 hosts 修改 - 技术小黑屋 Vs Code 快速实现 重写 方法 Merge(Pull) Request 推荐的标签列表 - 技术小黑屋 中国特惠!多平台广告屏蔽专家 AdGuard 买断仅需 119 元起 使用 FVM 解决 flutter 3 无法添加 uploader 问题 AAPT2 aapt2-7.2.2-7984345-osx Daemon #5: Idle daemon unexpectedly exit. This should not happen 问题解决 git clone 使用代理,实现百倍加速 - 技术小黑屋 Flutter 处理 Error Setter not found AsciiChar 问题
使用 flock 解决 Git `unable to read tree` 问题
androidyue · 2025-06-15 · via 技术小黑屋

背景

在 CI/CD 环境下,团队常遇到以下错误:

1
fatal: unable to read tree <SHA>

这通常是多个进程或脚本并发操作同一个 Git 仓库,导致元数据损坏或锁冲突。Git 并非为高并发本地操作设计,因此需要解决并发问题。

问题复现

在自动化脚本中,例如:

1
2
git fetch origin
git checkout some-branch

如果多个任务同时执行,可能导致锁冲突或元数据损坏。

解决思路

通过加锁机制,让所有 Git 操作串行执行。flock 是一个简单高效的工具,专为这种场景设计。

flock 安装

Linux

大多数 Linux 发行版自带 flock(属于 util-linux 套件)。如果没有,可按以下方式安装:

  • Debian/Ubuntu:
1
2
sudo apt-get update
sudo apt-get install util-linux
  • CentOS/RHEL:
1
sudo yum install util-linux
  • Arch:
1
sudo pacman -S util-linux

安装后即可使用 flock 命令。

macOS

macOS 默认不包含 flock,但可通过 Homebrew 安装兼容版本:

安装的是 Ben Noordhuis 的 flock,语法与 Linux 版本基本一致。

提示:在 CI 服务(如 GitHub Actions)中,可在步骤中提前安装 flock

flock 用法

flock 用于在 shell 脚本中对文件加锁:

1
flock <lockfile> <command>

建议将锁文件放在 .git 目录下,避免污染业务代码目录。

实战例子

假设有一个 deploy.sh 脚本:

1
2
3
4
#!/bin/bash
git fetch origin
git checkout some-branch
# ...more commands...

加锁后修改为:

1
2
3
4
5
6
7
8
#!/bin/bash
LOCK_FILE="/path/to/your/repo/.git/deploy.lock"

flock -n "$LOCK_FILE" bash <<'EOF'
git fetch origin
git checkout some-branch
# ...more commands...
EOF

或者直接锁定整个脚本:

1
flock -n /path/to/your/repo/.git/deploy.lock ./deploy.sh
  • -n:表示拿不到锁时立即退出(可选)。
  • 建议将锁文件放在 .git 目录下。

总结

  • 避免并发操作同一个 Git 仓库!
  • 使用 flock 使 Git 操作串行,防止元数据损坏。
  • Linux 下直接使用,macOS 通过 Homebrew 安装 flock
  • 锁粒度可适当放宽,确保安全优先。
  • 本地自动化操作 Git 时,flock 是必备工具,简单高效!

如有问题,请在评论区讨论。