问题
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:
In my case, this was caused by using the same Git repo from both admin and non-admin command prompts. When last git pull
was from admin cmd, the index
was created by it, and then non-admin cmd had insufficient permissions to modify it.
My solution was re-creating the index
(while keeping the worktree intact):
del .git\index
git reset --mixed head
回答3:
I was seeing this Rename from '.git/index.lock'...
message when attempting to execute
git checkout -b my-branch
The fix for me was to run the command line as admin
.
Specifically I was using the excellent cmder application as a non-admin, which resulted in the rename message appearing. By running cmder as an admin, then performing the checkout again, it worked fine.
回答4:
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 processesWhen 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 thehStdXxx
handles inSTARTUPINFO
(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 toO_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.
回答5:
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"
回答6:
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
回答7:
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.
回答8:
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
回答9:
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.
回答10:
It can be a right issue, try to run your Terminal as Administrator instead of user. Worked for me
回答11:
I'm using Tortoise Git. I just opened a new Windows Explorer and it fixed this. (For command line Git maybe just open a new shell).
回答12:
I had seem issue when I was rebase my branch with master. My solution is turn off all solution which are opening and reset hard my branch to origin and rebase again.
回答13:
The cause 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.
Tunr on antivirus.
It will work.
来源:https://stackoverflow.com/questions/13635429/git-rename-from-index-lock-to-index-failed