剩余的index.lock
文件
当你有剩余的index.lock
文件时,git status
命令可能会变得非常缓慢。
特别是在使用git子模块
时,这种情况经常会出现,因为你通常不会注意到这些残留文件。
总结:在检查这些文件确实未被任何当前正在运行的程序使用后,请运行find .git/ -name index.lock
命令并删除剩余文件。
详细信息
我发现我的shell git状态在Ubuntu 16.04上的git 2.19版本下非常缓慢。
我深入研究后发现,我的assets
git子模块中的/usr/bin/time git status
需要1.7秒。
使用strace
发现git使用mmap
读取了其中所有大文件。通常情况下,使用stat
就足够了。
我搜索了这个问题并找到了使用索引和Racy Git问题。
尝试使用git update-index somefile
(在我的情况下是子模块检出中的gitignore
),如此处所示,但失败了。
fatal: Unable to create '/home/niklas/src/myproject/.git/modules/assets/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.
这是一个经典错误。通常在任何git操作中都会注意到它,但对于你不经常提交的子模块,可能几个月都不会注意到,因为它只出现在将某些内容添加到索引时;警告不会在只读的
git status
上引发。
删除
index.lock
文件后,
git status
立即变快,
mmaps
消失了,现在快了1000倍以上。
因此,如果你的git状态异常缓慢,请检查
find .git/ -name index.lock
并删除残留文件。