文章摘要
Claude Code存在一个严重问题:它会每隔10分钟自动对项目仓库执行"git reset --hard origin/main"命令,强制重置本地修改。这一行为可能导致开发者未提交的代码丢失,引发数据风险。该问题已被记录在GitHub仓库的Issue #40710中,需要尽快修复以避免影响开发工作。
文章总结
文章主要内容重述
标题:Claude Code 每10分钟对项目仓库执行 git reset --hard origin/main 操作
来源:GitHub 问题 #40710
发布时间:2026年3月29日
问题描述
用户报告称,Claude Code 会定期(每10分钟)对项目仓库执行 git fetch origin 和 git reset --hard origin/main 操作,导致未提交的更改被静默覆盖。未跟踪的文件不受影响,而 Git 工作树(worktree)则完全免疫此问题。
环境信息
- Claude Code 版本:2.1.87(通过 Homebrew 安装的 Bun 二进制文件)
- 操作系统:macOS 15.4(Darwin 25.3.0,arm64)
- Shell:zsh
证据与排查
- Git 日志:
git reflog显示大量记录,时间间隔严格为10分钟,表明存在定时任务。 - 现场复现:修改文件后,10分钟后文件被自动恢复,未跟踪文件则保留。
- 文件监控:
fswatch捕获到.git/目录的锁定文件操作,符合git reset --hard的行为特征。 - 进程排查:
lsof确认只有 Claude Code 进程在操作目标仓库,且未调用外部git命令,说明是内部程序化操作(可能通过 libgit2 实现)。 - 工作树免疫:Git 工作树未受此问题影响。
排除的外部因素
- Git 钩子、IDE/编辑器、云同步工具(如 iCloud、Dropbox)、定时任务(Cron/LaunchAgents)、开发服务器(如 Vite/SvelteKit)等均被排除。
二进制分析(部分)
从编译后的二进制文件中发现:
- hg1() 函数执行 fetch origin 操作,默认使用 process.cwd() 作为工作目录。
- io1() 函数是 git pull 的封装。
- 未明确找到定时器设置的代码(可能因代码压缩难以定位)。
影响
未提交的更改会定期丢失,问题在提交后不可见,导致用户误以为是间歇性故障。
用户疑问
Claude Code 团队:是什么内部机制每600秒对 process.cwd() 执行 git reset --hard?
临时解决方案
- 使用 Git 工作树(完全免疫)。
- 频繁提交更改(已提交的内容不受影响)。
相关 issue
- #8072:代码修改被反复恢复
- #7232:未经授权的
git reset --hard导致数据丢失 - #32793:
claude install错误修改 Git 远程仓库地址
社区反馈
用户建议检查 MEMORY.md 或 .claude/ 目录中是否包含 sleep 或 600 的配置,以排查可能的错误配置。
(注:原文中大量导航菜单、页脚信息等与问题无关的内容已删减,仅保留核心问题描述和讨论部分。)
评论总结
以下是评论内容的总结:
对AI潜在风险的担忧
多位用户对AI系统可能执行破坏性操作表示担忧,认为自然语言指令可能引发意外后果。- "The idea a natural request can get Claude to invoke potentially destructive actions on a timer is silly"
- "cool. if you choose to use a non-deterministic black box of bullshit, should you really be surprised when it shits all over your floor?"
概率性系统的固有缺陷
有评论指出这是概率性系统的必然结果,即使低失败率在大规模使用时也会显现。- "Even a 0.01% failure rate...would show up regularly at scale"
- "The model is probabilistic and sequences like
git reset --hardare very common in training data"
权限管理建议
部分用户建议通过权限设置来防止问题发生。- "They should run with permissions and a ruleset"
- "Always configure remote to reject any kind of trunk push"
对问题普遍性的质疑
有评论认为这可能是个别案例,不应过度解读。- "I think this post potentially mischaracterises what may be a one off issue"
- "I'm curious how common this is or if this just affects this one user"
幽默看待现象
部分用户以幽默态度看待这一现象。- "It's a feature not a bug!"
- "This whole LLM thing is a blast, huh?"
技术解决方案建议
有评论提出具体技术改进方案。- "I would replace the git on the $PATH by a wrapper that logs all operations"
- "The obvious solution is to just copy paste it into Claude itself and ask it to fix"
开发习惯批评
有评论指出长时间不提交代码是反模式。- "having stuff just sitting around uncommitted for days...is an anti-pattern"