windows Git 从 index.lock 重命名为 index 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13635429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:04:58  来源:igfitidea点击:

Git rename from index.lock to index failed

windowsgitwindows-7github

提问by Tom Hunter

Using the GitHub Windows client I did a syncto pull remote changes to my local machine, but before finishing the sync, I ran out of disk space and the sync failed. Now I seem to have a bunch of local changes that are actually changes that were being pulled from origin. I tried to run git pull but got:

使用 GitHub Windows 客户端,我进行了同步以将远程更改拉到我的本地机器上,但在完成同步之前,我用完了磁盘空间并且同步失败。现在我似乎有一堆本地更改,这些更改实际上是从源中提取的更改。我试图运行 git pull 但得到:

C:\Users\Tom\SourceLog [master +4 ~26 -0 !]> git pull
Updating b3a86e1..5afd74f
error: Your local changes to the following files would be overwritten by merge:
        SourceLog.Interface/IChangedFile.cs
        SourceLog.Interface/ILogEntry.cs
        ...
Please, commit your changes or stash them before you can merge.
error: The following untracked working tree files would be overwritten by merge:
        Lib/MSBuildExtensionPack/4.0.6.0/Ionic.Zip.dll
        Lib/MSBuildExtensionPack/4.0.6.0/MSBuild.ExtensionPack.dll
        ...
Aborting

So now I'm trying to discard the local changes but I'm getting:

所以现在我试图放弃本地更改,但我得到:

C:\Users\Tom\SourceLog [master +4 ~26 -0 !]> git checkout -- .
Rename from '.git/index.lock' to '.git/index' failed. Should I try again? (y/n) y
Rename from '.git/index.lock' to '.git/index' failed. Should I try again? (y/n) n
fatal: unable to write new index file

How can I clean this up? (I didn't have any local changes before starting the sync.)

我该如何清理?(在开始同步之前,我没有任何本地更改。)

Update

更新

Can't seem to reset head..

似乎无法重置头..

C:\Users\Tom\SourceLog [master +4 ~0 -0 !]> git reset head
Rename from '.git/index.lock' to '.git/index' failed. Should I try again? (y/n) y
Rename from '.git/index.lock' to '.git/index' failed. Should I try again? (y/n) n
error: Could not write new index file.
fatal: Could not reset index file to revision 'head'.

采纳答案by Tom Hunter

Looks like the following process had a lock on the .git\indexfile:

看起来以下进程锁定了.git\index文件:

ssh-agent.exe
C:\Users\Tom\AppData\Local\GitHub\PortableGit_8810fd5c2c79c73adcc73fd0825f3b32fdb816e7\bin\ssh-agent.exe

I killed the process and ran git reset HEADand looks like I'm back to normal now.

我终止了进程并运行git reset HEAD,现在看起来我已经恢复正常了。

回答by Dimagog

In my case, this was caused by using the same Git repo from both admin and non-admin command prompts. When last git pullwas from admin cmd, the indexwas created by it, and then non-admin cmd had insufficient permissions to modify it.

就我而言,这是由于在管理员和非管理员命令提示符下使用相同的 Git 存储库造成的。当最后一个git pull来自admin cmd时,它index是由它创建的,然后非admin cmd没有足够的权限来修改它。

My solution was re-creating the index(while keeping the worktree intact):

我的解决方案是重新创建index(同时保持工作树完好无损):

del .git\index
git reset --mixed head

回答by Jason Evans

I was seeing this Rename from '.git/index.lock'...message when attempting to execute

Rename from '.git/index.lock'...在尝试执行时看到了这条消息

git checkout -b my-branch

git checkout -b my-branch

The fix for me was to run the command line as admin.

我的解决方法是将命令行作为admin.

Specifically I was using the excellent cmderapplication as a non-admin, which resulted in the rename message appearing. By running cmderas an admin, then performing the checkout again, it worked fine.

具体来说,我以非管理员身份使用优秀的cmder应用程序,这导致出现重命名消息。通过以管理员身份运行cmder,然后再次执行结帐,它运行良好。

回答by VonC

Git 2.10 (Q3 2016, 4 years later) should improve the situation on Windows

Git 2.10(2016 年第三季度,4 年后)应该会改善 Windows 上的情况

See commit 05d1ed6(23 Aug 2016) by Ben Wijen (Ben).

请参阅Ben Wijen ( )提交的 05d1ed6(2016 年 8 月 23 日Ben

mingw: ensure temporary file handles are not inherited by child processes

When the index is locked and child processes inherit the handle to said lock and the parent process wants to remove the lock before the child process exits, on Windows there is a problem: it won't work because files cannot be deleted if a process holds a handle on them.

The symptom:

mingw: 确保子进程不会继承临时文件句柄

当索引被锁定并且子进程继承了该锁的句柄并且父进程想要​​在子进程退出之前移除该锁时,在 Windows 上会出现一个问题:它不会工作,因为如果进程持有文件将无法删除对他们的处理。

症状:

Rename from 'xxx/.git/index.lock' to 'xxx/.git/index' failed.
Should I try again? (y/n)

Spawning child processes with bInheritHandles==FALSEwould not work because no file handles would be inherited, not even the hStdXxxhandles in STARTUPINFO(stdin/stdout/stderr).

Opening every file with O_NOINHERITdoes not work, either, as e.g. git-upload-packexpects inherited file handles.

This leaves us with the only way out: creating temp files with the O_NOINHERITflag. This flag is Windows-specific, however.
For our purposes, it is equivalent to O_CLOEXEC(which does not exist on Windows), so let's just open temporary files with the O_CLOEXECflagand map that flag to O_NOINHERITon Windows.

生成子进程bInheritHandles==FALSE将不起作用,因为不会继承任何文件句柄,甚至不会继承(stdin/stdout/stderr) 中的hStdXxx句柄STARTUPINFO

打开每个文件O_NOINHERIT也不起作用,例如git-upload-pack期望继承的文件句柄。

这给我们留下了唯一的出路:创建带有O_NOINHERIT标志的临时文件。但是,此标志是特定于 Windows 的。
对于我们而言,它相当于O_CLOEXEC(不存在于Windows),所以我们用只需打开临时文件O_CLOEXEC标志那个标志映射到O_NOINHERITWindows上

回答by krekto

I removed indexand index.lock(in the .gitfolder) and ran git checkout .to undo the changes and resolved, but if I wanted to commit the changes I would have run git add -Aafter git commit -m "description"

我删除了indexindex.lock(在.git文件夹中)并运行git checkout .以撤消更改并解决了,但是如果我想提交更改,我会在git add -A之后运行git commit -m "description"

回答by HamzDiou

It can be a right issue, try to run your Terminal as Administrator instead of user. Worked for me

这可能是一个正确的问题,尝试以管理员身份而不是用户身份运行您的终端。对我来说有效

回答by lietus

For me it was this error:

对我来说这是这个错误:

Rename from 'D:/dev/repo/.git/refs/remotes/origin/my-branch.lock' to 'D:/dev/repo/.git/refs/remotes/origin/my-branch' failed. Should I try again? (y/n)

Rename from 'D:/dev/repo/.git/refs/remotes/origin/my-branch.lock' to 'D:/dev/repo/.git/refs/remotes/origin/my-branch' failed. Should I try again? (y/n)

Renamed "my-branch" file, retried, and "my-branch.lock" succeed in renaming, not sure if this is correct, but worked. Local changes in both master and my-branch were preserved.

重命名“my-branch”文件,重试,“my-branch.lock”重命名成功,不确定这是否正确,但有效。保留了 master 和 my-branch 中的本地更改。

回答by thanos.a

In my case I had to close the VS code which I opened with code .from a WSL Ubuntu cli.

就我而言,我不得不关闭code .从 WSL Ubuntu cli打开的 VS 代码。

回答by Rakesh Singh Balhara

git initresolved my problem. I was getting below issue.

git init解决了我的问题。我得到了以下问题。

Rename from '.git/objects/pack/pack-XXXXX.pack' to '.git/objects/pack/old-XXXXX.pack' failed. Should I try again? (y/n)

Rename from '.git/objects/pack/pack-XXXXX.pack' to '.git/objects/pack/old-XXXXX.pack' failed. Should I try again? (y/n)

回答by JavidKhalil

This is caused when antivirus or OS defender (for example Windows Defender) is running. The solution: turn off antivirus for several minutes make your add, commit and push. Turn on antivirus.
It will work.

这是在防病毒软件或操作系统防御程序(例如 Windows Defender)运行时引起的。解决方案:关闭杀毒软件几分钟,让你添加、提交和推送。打开防病毒软件。
它会起作用。