Skip to content

Cannot Lock Ref

错误信息

                fatal: cannot lock ref 'refs/heads/feature': unable 到 resolve 引用
              

快速摘要

引用 lock 能不是获取 — 通常 due 到 stale lock file or corrupted ref 状态.

为什么发生

  • 上一个 Git 操作是 interrupted, leaving stale lock file

  • Another Git 处理是运行 simultaneously on 相同仓库

  • .git directory 有 corrupted ref files

最小示例

✗ 错误代码
git checkout feature  # fatal: cannot lock ref 'refs/heads/feature'
✓ 修复代码
# Remove stale lock file
rm .git/refs/heads/feature.lock
git checkout feature  # now succeeds

# Or clean up with gc
git gc
git checkout feature

移除 stale .lock file 左通过 interrupted 操作, 然后 retry command.

如何诊断

  • 检查 for lock files: ls .git/refs/heads/ and look for .lock files

  • 验证否 other Git 处理是运行

  • 运行 git gc 到 clean 上 and pack refs

如何修复

  • 移除 stale lock files: rm .git/refs/heads/<分支>.lock (use 使用注意)

  • 运行 git gc 到 clean 上仓库

  • 如果 corrupted, re-克隆仓库从 remote

如何预防

  • Avoid 运行多个 Git 操作 simultaneously on 相同仓库

  • 做不 interrupt Git 操作 mid-execution

相关资源

相关术语

相关课程

← 返回语言