Git rename from index.lock to index failed

匿名 (未验证) 提交于 2019-12-03 02:12:02

问题:

Using the GitHub Windows client I did a sync to 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:

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'. 

回答1:

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

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

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



回答2:

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

del .git\index git reset --mixed head 


回答3:

To discard local changes, go

git reset HEAD 

Then checkout your old commit, delete the new one, and pull again.

git checkout "hashOld" git branch -d "hashNew" git pull 


回答4:

I had a similar issue with Git. The solution for me was to delete the solution locally through windows explorer, and then re-clone the repository. This removed all the files that were stored locally on my machine, and resulted in the

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

going away. After I cloned the respository, I tried my command again(which in my case was GIT COMMIT) and the failure did not reoccur.

The issue came about when I was trying to resolve a merge conflict that was happening after merging a feature branch into the develop branch.



回答5:

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

See commit 05d1ed6 (23 Aug 2016) by Ben Wijen (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:

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

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

Opening every file with O_NOINHERIT does not work, either, as e.g. git-upload-pack expects inherited file handles.

This leaves us with the only way out: creating temp files with the O_NOINHERIT flag. 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_CLOEXEC flag and map that flag to O_NOINHERIT on Windows.



回答6:

Either kill the process that is locking the file or if it is a new repo, del the .git folder rm -rf .git and start again with git init



回答7:

I got this error several times in a row when running git reset HEAD in a project stored in a Google Drive folder, but after a few minutes the problem went away.



回答8:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!