73

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'.
Tom Hunter
  • 5,714
  • 10
  • 51
  • 76
  • 2
    Have you freed up some space in the interim? That would be one reason that a rename fails. If that's not the reason rename is failing, you might try deleting `.git/index` (or move it somewhere else, just in case), and try `git reset --hard HEAD` again – twalberg Nov 29 '12 at 22:22
  • Yeah I cleared a few gigs of space. Tried moving the index file and discovered a process had a lock on it.. – Tom Hunter Nov 29 '12 at 22:38
  • 1
    delete .git/index worked for me . thanks. it just recreates it. – ramnz Sep 30 '13 at 23:05
  • Once GitHub for Windows will use git 2.8.4 (June 2016), you should not see that error anymore. [See my report here](http://stackoverflow.com/a/37710754/6309)) – VonC Jun 08 '16 at 19:01

26 Answers26

39

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
Dimagog
  • 1,785
  • 19
  • 14
  • 4
    Worked for me by deleting both .git/index and .git/index.lock – Matt Mar 13 '18 at 10:36
  • 2
    Hi, normally it shouldn't be necessary to specify `--mixed`, as it is the standard already: https://git-scm.com/docs/git-reset#git-reset---mixed – A. Blesius Oct 16 '18 at 07:03
  • 3
    @A.Blesius yes, `--mixed` is the default, but I made it explicit because `reset` command could look scary. – Dimagog Feb 04 '19 at 20:15
  • 1
    @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! – Lightning3 Jun 22 '20 at 09:20
  • After doing this, I was able to `git add .`, but got the error again when doing `git commit` – AlwaysLearning Dec 18 '21 at 19:57
38

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.

Tom Hunter
  • 5,714
  • 10
  • 51
  • 76
  • 7
    I guess it's not even required to do `git reset HEAD`. It worked for me without that! – BrainStone Jun 18 '13 at 20:57
  • 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. – Rob Oct 23 '13 at 17:34
  • 6
    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. – Olov Dec 02 '19 at 10:55
  • 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. – Heinrich Ulbricht May 19 '21 at 06:36
34

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

thanos.a
  • 2,246
  • 3
  • 33
  • 29
  • 3
    exactly what happened to me. but it's still a pain that I have to close vscode to do a git commit. – Laurence Apr 01 '21 at 15:02
13

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

HamzDiou
  • 588
  • 9
  • 15
7

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.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
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 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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

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
James McDonnell
  • 3,600
  • 1
  • 20
  • 26
2

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)

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.

lietus
  • 199
  • 2
  • 11
1

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"

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
krekto
  • 1,403
  • 14
  • 18
1

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.

Brian Burns
  • 20,575
  • 8
  • 83
  • 77
  • Ah, also using Google Drive. Seems to go away after a second or two. – 101 Jul 18 '18 at 01:31
  • 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. – Elliott Collins Jul 08 '20 at 22:14
1

I ran into this issue and wanted to post the answer for future searchers. Windows confirmed only. I got this when my git repository was under a directory that requires elevated permissions, AND I was running git from a process(cmd.exe) that did NOT have permissions to write to that folder, thus it could not clear the lock.

The answer is simple, run as admin.

apinostomberry
  • 139
  • 1
  • 11
1

I've answered this on a similar question. Might help others too, quoting the same here:

"For if any windows user stumbles on this:

I faced same issue and it wasn't solved by permissions since I had all the permissions assigned. Removing index.lock didn't help either.

I tried with WSL and it showed this error:

Another git process seems to be running in this repository.

Based on this I quit every process run by VS Code (that is the IDE I am using) and voila everything back to normal.

Note: Merely closing VS Code didn't help, had to end all processes from task explorer.

On a larger point, try ending all the processes via which you were using git. For me it was just VS Code."

Original answer: https://stackoverflow.com/a/67615831/13404308

nu_popli
  • 920
  • 1
  • 7
  • 12
1

Restart your Source Code editor; killing a Process may work but is not polite_

0

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.

Judy007
  • 5,484
  • 4
  • 46
  • 68
0

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

Robot Boy
  • 1,856
  • 1
  • 17
  • 17
0

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.

Tanque
  • 315
  • 3
  • 16
  • try to highlight the keywords and be clear with the format it will help to reach out your answer for others – Agilanbu Dec 22 '18 at 16:19
0

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

Mark Seagoe
  • 480
  • 8
  • 15
0

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.

0

git init resolved my problem. I was getting below issue.

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

0

In my case, I had done a git pull as Admin, and my regular user no longer had sufficient permissions.

My solution was to reset ownership to my regular user.

  1. Right-click on .git, and open Properties.
  2. On the Security tab, click Advanced.
  3. In Advanced Security Settings, next to Owner, click Change (even if it's already correct).
  4. In Select User, select the regular user, and click Okay.
  5. On the Security tab, under Owner, you now have a Replace owner of subcontainers and objects checkbox - Check this and click Okay.
chicsa
  • 1
0

I tried a lot of suggestions. At times, a commit would succeed, but then the error returned. At last, pausing Dropbox sync seems to have done the trick! I have no clue how Dropbox sync could cause this error for longer than a few moments (the time required to sync my 24K .git/index).

AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
0

For my case, it was just Windows Explorer being open and it was opening the directory above the directory that I wanted to rename. After closing Windows Explorer, issue went away immediately.

I was using Bash Git as Administrator.

bobt
  • 324
  • 3
  • 3
0

in my case, I was using vim on WSL and writing git command on Powershell so I quit vim and then I was able to switch branches

Moaad Ben
  • 15
  • 5
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32419048) – OznOg Aug 12 '22 at 09:49
0

On Windows I resolved this by openeing powershell as admin and doing a git status in the folder and closing it. Then my other terminals/apps were able to use git again without any issues.

Brad
  • 741
  • 7
  • 17
-1

I copied (in Windows 10) the whole project to a new folder. Pushed from there to the origin (at that no index problem issued). Then I returned back to the project directory, where the problem occured. Deleted .git directory. Initialized anew (git init). Added to the index (git add . ). Fetched from the remote origin and merged. That's all there is to it.

Yuri
  • 1
-1

I change project permission

enter image description here

Then, I exit Code Editor

robocon321
  • 363
  • 5
  • 8