首页 > 解决方案 > 我无法使用 Visual Studio 代码将文件推送或提交到 github

问题描述

我尝试使用此问题的重复项来解决此问题,但到目前为止没有任何效果。

Git Status 产生这些结果

$ git status
On branch main
Your branch and 'origin/main' have diverged,
and have 15 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
        deleted:    rockpaperscissors

当我尝试在主分支中提交更改时,它会显示以下内容

$ git commit
fatal: Unable to create 'C:/Users/Dume/rps/.git/index.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.

我删除了 VSC 中的 index.lock 文件,但它似乎不断回来

我也尝试过完全删除 Github 中的存储库并用新的存储库替换它,但没有任何改变。

我不知道下一步该做什么

标签: gitgithubvisual-studio-codegithub-for-windows

解决方案


索引是 Git 的一部分,用于跟踪哪些文件要为下一次提交暂存。当 Git 因为某个程序正在更新它而想要写入索引的新副本时,它会创建文件index.lock,如果它已经存在则失败,将新内容写入其中,然后用新文件原子地替换旧索引。

您看到此index.lock文件持续存在的原因有几个:

  • 您系统上的某些东西正在尝试更新索引并且从未完成。这可能是某个使用长时间运行的进程调用 Git 的工具,或者是一个程序,可能是您的编辑器,它使用 libgit2 在磁盘上创建一个索引,然后从不提交它。
  • 您系统上的某些 Git 实用程序实际上正在崩溃。这可能不太可能。
  • 您系统上的某些东西正在阻止原子替换工作,这可能会留下index.lock文件。Windows 有一堆奇怪的规则,关于何时可以和不能进行原子替换,这些规则在 Unix 上不存在。

我的猜测是系统上有一些其他进程干扰了您的存储库。如果您的存储库位于云同步存储桶中(例如 OneDrive 或 Dropbox),这可能是原因;众所周知,它们会破坏事物并破坏存储库。它可能是除默认设置(Windows Defender 和 Windows 防火墙)之外的某种防病毒或防火墙,或其他监控系统内容的工具。

您的编辑器、编辑器 Git 集成或编辑器插件也可能导致问题。真的只有大量的可能性,很难说。

我肯定会将所有存储库移出云同步存储,仅使用 Windows Defender 和 Windows 防火墙,并尝试仅从命令行访问 Git,关闭任何编辑器集成,看看你是否仍然看到问题。如果您仍然这样做,请查找任何可能为您提供线索的错误消息,并尝试将行为与此时发生的其他任何事情相关联。


推荐阅读