You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Eclipse中向GitHub提交时添加文件到索引报错问题?

解决EGit添加资源到索引失败(LockFailedException)的问题

核心问题分析

从错误日志可定位根本原因:

org.eclipse.jgit.errors.LockFailedException: Cannot lock C:\Users\motah\.git\index. Ensure that no other process has an open file handle on the lock file C:\Users\motah\.git\index.lock, then you may delete the lock file and retry.

这是Git索引锁文件被占用或残留导致的,与项目本身无关,因此删除重建项目无法解决问题。

解决步骤

  • 关闭所有可能访问该Git仓库的程序:包括Eclipse、Git Bash、GitHub Desktop、VS Code等任何操作Git的工具
  • 手动删除锁文件:找到路径C:\Users\motah\.git\index.lock,直接删除该文件(若找不到,先开启文件管理器「显示隐藏文件」选项)
  • 重启Eclipse,重新执行「添加到索引」操作
  • 若问题依旧,检查.git文件夹权限:确保当前Windows用户拥有该文件夹的读写权限
  • 备选方案:用Git命令行完成添加操作
    1. 打开Git Bash,进入项目根目录
    2. 执行命令 git add .
    3. 返回Eclipse,右键项目 → Team → 刷新,即可看到索引已更新

完整错误日志

!ENTRY org.eclipse.egit.ui 4 0 2023-01-25 20:38:42.622
!MESSAGE Failed to add resource to index
!STACK 1
org.eclipse.core.runtime.CoreException: Failed to add resource to index
    at org.eclipse.egit.core.op.AddToIndexOperation.execute(AddToIndexOperation.java:78)
    at org.eclipse.egit.ui.internal.actions.AddToIndexActionHandler$1.run(AddToIndexActionHandler.java:59)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: org.eclipse.jgit.api.errors.JGitInternalException: Exception caught during execution of add command
    at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:225)
    at org.eclipse.egit.core.op.AddToIndexOperation.execute(AddToIndexOperation.java:74)
    ... 2 more
Caused by: org.eclipse.jgit.errors.LockFailedException: Cannot lock C:\Users\motah\.git\index. Ensure that no other process has an open file handle on the lock file C:\Users\motah\.git\index.lock, then you may delete the lock file and retry.
    at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:221)
    at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:292)
    at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:258)
    at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:1285)
    at org.eclipse.egit.core.RepositoryHandle.lockDirCache(RepositoryHandle.java:319)
    at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:122)
    ... 3 more
!SUBENTRY 1 org.eclipse.egit.core 4 0 2023-01-25 20:38:42.622
!MESSAGE Failed to add resource to index
!STACK 0
org.eclipse.jgit.api.errors.JGitInternalException: Exception caught during execution of add command
    at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:225)
    at org.eclipse.egit.core.op.AddToIndexOperation.execute(AddToIndexOperation.java:74)
    at org.eclipse.egit.ui.internal.actions.AddToIndexActionHandler$1.run(AddToIndexActionHandler.java:59)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: org.eclipse.jgit.errors.LockFailedException: Cannot lock C:\Users\motah\.git\index. Ensure that no other process has an open file handle on the lock file C:\Users\motah\.git\index.lock, then you may delete the lock file and retry.
    at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:221)
    at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:292)
    at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:258)
    at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:1285)
    at org.eclipse.egit.core.RepositoryHandle.lockDirCache(RepositoryHandle.java:319)
    at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:122)
    ... 3 more

内容的提问来源于stack exchange,提问作者Moti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 12:20:36