【问题标题】:fatal: cannot lock ref 'HEAD': Unable to create 'G:/folder/.git/HEAD.lock': File exists致命:无法锁定 ref 'HEAD':无法创建 'G:/folder/.git/HEAD.lock':文件存在
【发布时间】:2020-10-18 13:23:00
【问题描述】:

我不小心停止了正在进行的 git 提交,后来当我尝试时

git commit -m"xyz" 

我收到以下消息

fatal: cannot lock ref 'HEAD': Unable to create 'G:/folder/.git/HEAD.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.

我尝试按照这里给出的建议Git cannot lock ref 'HEAD': unable to resolve reference HEAD 并尝试过

 rm -rf .git/refs/heads/

不幸的是,这没有帮助,当我尝试 git commit 时,我仍然收到上面发布的相同错误消息。

我也尝试了 git reset ,这也给出了上面显示的相同消息。

有关如何解决此问题的其他建议将非常有帮助。

【问题讨论】:

  • 哇,rm -rf .git/refs/heads/ 基本上破坏了您的存储库!如果没有,您要么没有任何提交,要么刚刚克隆了存储库,或者您刚刚完成了git gc
  • .git/log 下仍然可以找到引用的位置(名称和最后一次活动提交)
  • @LeGEC:至少在我的机器上是带有 s.git/logs。我很好奇,并尝试自己以这种方式创建一个 repo,而 git reflog 在这种状态下不适用于存储库,很高兴知道。
  • 请定义“停止正在进行的提交”。怎么样?
  • @DaemonPainter 我做了一个 ctrl + C

标签: git


【解决方案1】:

[这是对“我如何恢复rm -rf .git/refs/heads”的回答]

您可以通过直接从日志文件中读取分支名称和上次活动提交来恢复您的分支:

find .git/logs/refs/heads -type f | while read log; do
  ref=$(echo $log | sed -e 's,logs/,,')
  mkdir -p $(dirname $ref)
  echo $(tail -1 $log | cut -d' ' -f2) > $ref
done

在带有bash 的linux-ey 系统上运行上述程序,或者在Windows 上使用git-bash


一步一步:

  • git 将每个分支/引用的演变存储在一个文件中,位于.git/logs 下。第一个命令列出了这些文件的分支(引用以refs/heads 开头):
$ find .git/logs/refs/heads -type f
.git/logs/refs/heads/master
.git/logs/refs/heads/foobar
.git/logs/refs/heads/baz/2
.git/logs/refs/heads/baz/1

您可以查看以下任何文件的内容:

$ cat .git/logs/refs/heads/baz/2
0000000... 219145b... Legec <some@email.com> 1603086835 +0200   branch: Created from master
219145b... 425bc45... Legec <some@email.com> 1603086837 +0200   commit: d.txt
425bc45... 84324a8... Legec <some@email.com> 1603087374 +0200   commit: e.txt
84324a8... 5c2e4de... Legec <some@email.com> 1603087380 +0200   rebase (finish): refs/heads/baz/2 onto cbe105c...

每一行都存储“引用从 xxx 移动到 yyy”,并带有一条消息,描述了触发这种演变的原因(提交、变基、重置……)

这是git reflog that/branch 从中读取其信息的文件。

  • 要获得实际参考的路径,您只需从该路径中删除 logs/ 部分:

    ref=$(echo $log | sed -e 's,logs/,,')
    
  • 假设您的分支名为thats/my/branch/name,您需要在.git/refs/heads/thats/my/branch/ 重新创建一个目录,然后再在其中创建文件name

    mkdir -p $(dirname $ref)
    
  • 最后:从日志文件的最后一行 (tail -1 $log) 提取第二个字段 (cut -d ' ' -f2) 并将其存储在适当的参考文件中:

    echo $(tail -1 $log | cut -d' ' -f2) > $ref
    

【讨论】:

    【解决方案2】:

    只需导航到克隆目录中的本地存储库并查找 .git 文件夹,在 .git 中您将看到锁定文件。删除它。你就完成了。

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 2016-12-27
      • 2013-05-12
      • 2019-04-12
      • 2016-09-02
      • 2012-12-09
      • 2011-12-13
      • 2023-03-18
      • 2018-06-19
      相关资源
      最近更新 更多