Git rename from index.lock to index failed

71,589

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

Solution 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
del .git\index.lock
git reset --mixed head

Solution 3

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

Solution 4

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

Solution 5

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.

Share:
71,589
Tom Hunter
Author by

Tom Hunter

SQL Server / C# developer, currently living and working in New York.

Updated on January 05, 2022

Comments

  • Tom Hunter
    Tom Hunter over 2 years

    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'.
    
  • James McDonnell
    James McDonnell over 11 years
    Or read here, this should work: stackoverflow.com/questions/3380805/…
  • Tom Hunter
    Tom Hunter over 11 years
    Thanks @JamesMcDonnell. Unfortunately it doesn't look like I can do this either.. (see my update).
  • James McDonnell
    James McDonnell over 11 years
    You could delete your .git folder, create a new repo and pull.
  • BrainStone
    BrainStone almost 11 years
    I guess it's not even required to do git reset HEAD. It worked for me without that!
  • Rob
    Rob over 10 years
    Agreed, I think it's just the ssh-agent.exe process that locks the index file. No need to do anything other than kill it.
  • Matt
    Matt about 6 years
    Worked for me by deleting both .git/index and .git/index.lock
  • 101
    101 almost 6 years
    Ah, also using Google Drive. Seems to go away after a second or two.
  • A. Blesius
    A. Blesius over 5 years
    Hi, normally it shouldn't be necessary to specify --mixed, as it is the standard already: git-scm.com/docs/git-reset#git-reset---mixed
  • Agilanbu
    Agilanbu over 5 years
    try to highlight the keywords and be clear with the format it will help to reach out your answer for others
  • Dimagog
    Dimagog about 5 years
    @A.Blesius yes, --mixed is the default, but I made it explicit because reset command could look scary.
  • Olov
    Olov over 4 years
    I encountered the same problem, and the process which had a lock for me was Visual Studio Code (or something related to it), because when I shut it down, it worked as expected.
  • Lightning3
    Lightning3 almost 4 years
    @DimagogThanks you so much for the tip. In my case: Visual studio in admin mode, and GitKraken in non-admin mode, so I got the message in GitKraken, because of permissions! Thanks!
  • Elliott Collins
    Elliott Collins almost 4 years
    For posterity, version conflicts in Dropbox can also cause this, but it won't go away on its own. In either case, the "conflicted copy" of .git/index just needs to be removed.
  • Daniel Butler
    Daniel Butler over 3 years
    This was my issue. The place that .git was located was in a protected folder
  • Laurence
    Laurence about 3 years
    exactly what happened to me. but it's still a pain that I have to close vscode to do a git commit.
  • Heinrich Ulbricht
    Heinrich Ulbricht almost 3 years
    For me it was explorer.exe although I did not have the actual folder or any sub folder open. Closing all Explorer windows was enough to solve the problem for me.
  • Günter Zöchbauer
    Günter Zöchbauer over 2 years
    In my case closing VSCode fixed it.
  • AlwaysLearning
    AlwaysLearning over 2 years
    After doing this, I was able to git add ., but got the error again when doing git commit