How to solve '.git/index.lock': No such file or directory' error? - windows

Summary
I recently lost the ability to make any changes using Git on my Windows system. After a few days of not using my PC I come back to a non functional git so I have no idea what caused this issue.
Examples:
git add or git checkout => fatal: Unable to create 'dummy-repo/.git/index.lock': No such file or directory
git clone => fatal: could not create work tree dir 'DummyRepo': No such file or directory
As a side note, this problem occured after trying to start my Flutter application, which could not retrieve the packages because of an OS error where access was denied. I'm thinking this is either because of some write permissions being messed up or a certain process locking a file.
What I've tried so far:
Closing all files and rebooting PC
Uninstalling and reinstalling Git
Run Git Bash as an administrator
Changed system write permission to my user account for all files
Any help is appreciated!

On Windows, this may happen if you enabled the "Ransomware protection" in Setting > Windows security > Virus & threat protection.
You need to add an exclusion for git.exe, or move the project outside a protected folder.

I've solved it myself by changing the location of my project to C:\ instead of a way longer path. This was never a problem before but it works so I'm happy.

I did 2 things:
I found that there was a file named 'index' in '.git/' directory. I made a copy of it, and renamed the copy as 'index.lock'.
Under 'Ransomware protection', I temporarily switched off 'Controlled folder access'.
Problem solved.
After I am done, I switched 'Controlled folder access' back on.

Try creating that file in your .git directory, you can use these commands in Git Bash or linux/mac
cd .git
touch index.lock
On Windows CMD
cd .git
type nul > index.lock

Related

Cant clone git repository

first-time asker here. I have an issue with git, cant clone anything. If I don't specify where to clone, this will appear.
> git clone https://gitlab.corp.cz/username/project_name.git project_name
fatal: could not create work tree dir 'project_name': No such file or directory
If I first create a folder via explorer, then the error message is different.
> git clone https://gitlab.corp.cz/username/project_name.git project_name
Cloning into 'project_name'...
C:/Users/username/Documents/project/path/.git: No such file or directory
Mkdir say the same, though not sure if windows even have a mkdir. At least a month ago, it worked. Please help, even reinstalled windows, didn't help.
EDIT: It looks like it is not a git problem. I cannot create any file using nothing other than windows explorer. Checked and I have write permissions for this folder, so most likely a windows problem.
a) You must run the command in a directory where you have write permission;
b) Add git to the list of allowed applications:
1 - Select Start > Settings > Update & Security > Windows Security > Virus & threat protection.
2 - Under Virus & threat protection settings, select Manage settings.
3 - Under Controlled folder access, select Manage Controlled folder access.
4 - Switch the Controlled folder access setting to On or Off.
c) If that doesn't work, try running the terminal with the administrator.
If you are using WSL (Windows Subsystem for Linux) to execute this command in the terminal check if the linux user has permissions to create a new folder there or try doing it as root.
If you are using Powershell or the Command Prompt try running it as Administrator and check if cloning a repo will work.. if this works there is a problem with your permissions. In this case try to change your working path so if the folder you are trying to clone a repo is in "C:\Program Files" or "C:\Program Files (x86)" try cloning to another place as these paths cause problems.

openssh windows bad owner or permissions

I've installed openssh for windows and when I run ssh localhost I get
Bad owner or permissions on C:\Users\gary/.ssh/config
I've looked at these 2 questions https://superuser.com/questions/348694/bad-owner-or-permissions-error-using-cygwins-ssh-exe and https://serverfault.com/questions/253313/ssh-returns-bad-owner-or-permissions-on-ssh-config but none of the answers work for me. sshd is running as a service as the Local System user. I've run chmod 0600 C:\Users\gary\.ssh\config and chown gary C:\Users\gary\.ssh\config. I've also cleared the ACL by running setfacl -b C:\Users\gary\.ssh\config and then chmod 0600 C:\Users\gary\.ssh\config again. I've also tried changing the owner to SYSTEM and got the same error.
I'm not sure what else to do, is there anything wrong with my setup? I also have git installed which installed mingw, I deleted ssh and sshd from my git installation so they wouldn't be on my path.
Other commands I've run are
icacls "C:\Users\gary\.ssh\config" /setowner gary
chown -R gary:1049089 C:\Users\gary\.ssh
ls -la C:\Users\gary\.ssh\config shows
-rw-r--r-- 1 gary 1049089 229 Jan 3 14:43 'C:\Users\gary.ssh\config'
it keeps showing this even after changing the owner to SYSTEM, but in the file properties in file explorer it shows SYSTEM as the owner
This started popping up immediately after I created another user with Administrator privileges, and that account began inheriting access to my .ssh folder.
You do not need to change your permissions whatsoever.
Just go to .ssh, right-click Properties, Security Tab, Advanced. DISABLE INHERITANCE, then click on the Administrator user (the one that is not you) and Remove them. Apply. Done.
Use ssh client from Git instead of Windows inbuilt SSH client. E.g. set VS Code to use C:\Program Files\Git\usr\bin\ssh.exe instead of C:\Windows\System32\OpenSSH\ssh.exe.
Steps:
In VS Code navigated to [File] -> [Preferences] -> [Settings] -> Search remote.ssh.path
Input C:\Program Files\Git\usr\bin\ssh.exe
Alternatively:
Update PATH environment variable to point to Git bin before Windows System32.
Type "env" in Start bar to edit System (or account) environment variables.
Select Path and hit edit.
Add C:\Program Files\Git\usr\bin\ssh.exe to the list and move it to the top of the list.
Just got same issue after re-install windows. And easily fixed just by changing the file permissions to
SYSTEM & Administrators - Full Control
[your username] - Modify & as Owner
Note:
I'm still using Windows 10 built-in SSH client C:\Windows\System32\OpenSSH\ssh.exe & not using cygwin at all
For those still struggling with this, check this out: https://github.com/PowerShell/openssh-portable/pull/418. This was the case for me. It turns out that your computer should be named differently from your username... 🤷‍♂️ It will probably be fixed soon in future updates, because fix got into commit.
So again: if your computer name is the same as your username and you still haven't fixed this issue with permissions dialog, then probably renaming your computer could help.
Instead of using the properties box, you can use the one liner:
icacls .ssh /grant:r <yourUserName>:f /inheritance:r
/grant:r username:f -> grant and overwrite permissions, giving full permissions to username
/inheritance:r -> remove inherited permissions
Keep known_hosts writable with
icacls .ssh/known_hosts /grant:rw <username>:f /inheritance:r
I'm not sure what version of Windows you're running, but since this is recent I'd guess Windows 10. I recently found out that an OpenSSH client is installed by default as of the April 2018 update. I then found I had two instances of OpenSSH: the one I installed myself and the one Windows gave me. Uninstalling the one I had installed caused the error message you describe.
The solution that worked for me was to remove the user-installed OpenSSH as well as the C:\Users\username\.ssh folder, and let Windows 10 OpenSSH create the folder when you run the command the next time. I didn't have any configuration I was worried about losing, but if you do I'd suggest copying and pasting the contents of the files somewhere and recovering them afterwards.
Hope this helps!
Having the exact same issue today, this is how I solved it:
Go to C:\Users\username.ssh
Right-click the config file
Properties -> Security -> Advanced -> Disable Inheritance -> Disable
inheritance -> Remove all inherited permissions from this object ->
Apply -> Yes -> Ok -> Ok
Use FixUserFilePermissions.ps1 to fix permissions of client side files - keys and config files of current user.
git clone git#github.com:PowerShell/openssh-portable.git
cd openssh-portable/contrib/win32/openssh
.\FixUserFilePermissions.ps1 -Confirm:$false
On windows server this is due to permission problem. Need to remove access to other users for the following folders
.ssh - folder
Right click on this folder -> Select "Give access to" - > Click on "Remove Access"
Right click on this folder -> Select "properties" - > "Securities" - > Click on "Edit Permissions" - Remove other users except the ID you are logged in.
Repeat the same process for the folder under which you have .pem file. (Note: Keep .pem file in a separate folder)
For anyone, who still has troubles after applying the owner + modify (plus full control for admins): it did not work for me. Then I saw a solution to remove all other users (incl all admins), which did not help either.
This worked for me:
leave System and Administrators in place, with full control, as suggested above
leave the user itself in place, as owner, with modify, as suggested above
however, remove any other user or group. You probably need to go to advanced first, to disable inheritance of rights
after I removed an administrative user who was added by Windows after entering my folder (by passing through the UAC box), it worked for me again.
Hope this helps for anyone who encounters this specific issue :-)
If User is in Administrative group just keep configuration in
c:\programdata\ssh\ssh_config instead %USERPROFILE%.ssh\config, will work
after disabling inheritance, make sure you add your current user, else u cannot edit the file
For me it was fixed by running chmod 0644 config under ~/.ssh/. Earlier it was set to 755 which was causing "Bad owner or permissions on /home/home/.ssh/config"
I tried all the solutions above, and sadly still can't fix this issue. I'm pretty sure the permission of my ssh config is correct, this has been verified by the Explore GUI and the Get-Acl commands.
Then I finally find a way to solve it:
delete the entire .ssh folder and then open powershell and type ssh localhost. It will create a new .ssh folder for you, then you can apply the above permission tweaks(for me I only did one thing: disable inheritance).
So if other solutions doesn't work for you, maybe you can try this. Hope it's helpful.
PS: don't forget to backup your old .ssh folder before deleting it.
I was having this problem, and no amount of changing permissions or disabling inheritance on the config file would fix it. It turned out that it did not like my computer name and user name being the same, so I re-named my computer, allowed open ssh to re-create the config file, and the permissions are now correct. That was probably a bad idea to begin with, tbh.
I deleted C:\Users\user/.ssh/config and reran my stuff, then it worked.
However, if you have something valuable there, make a backup first, just in case!
After a domain change over, I started having this same problem. Went through all of the suggestions listed and nothing worked, including both chmod and chown solutions.
I ended up fixing the problem by copying the folder, pasting it, deleting the original, and then renaming it back to .ssh.
The problem seems from the files are owned/has-permission for more than one user.
1- Go to your ./ssh folder and for both config & id_rsa files. From the properties -> Security -> Advanced:
2- Make sure that the user that you are logged in with IS the only user there.
No group change or whatever,the first answer is right.Change to git ssh.exe
How?
uninstall win10's openssh in Settings
add path of git's ssh.exe to your Path
For me it was fixed by running chmod 0644 config under ~/.ssh/ when running WSL.
Rename the config file to something like config2
Open this file with notepad
Save As config (original name)
This worked for me.
I guess it was caused by the wrong path expression.
Bad owner or permissions on C:\Users\gary/.ssh/config
The /.ssh should be \.ssh. So I try to use git bash (the terminal tool when install git in Windows system) to run ssh command. It really works. But I don't really know if it is caused by the reason I guessed.
Hi guys after a troubleshoot for a day I found that this "m.. f.." config file should not stand in the .ssh/ path.
For VSCODE just set the config in 'C:\ProgrmaData\ssh\ssh_config' path as proposed in the second choice of the palette command, and forget .shh path for this configuration.
That worked fine for me.
Nota: there was also a known_host file also created here with strange VM names inside, I deleted also this file. and that helps
For me, re-editing the permission settings in Windows is too complicated. Regenerating another configuration in vscode does not work either.
I set a custom config file path to solve this problem.
["Remote SSH: Config file"]
The absolute file path to a custom SSH config file.
note: search this option by #ext:ms-vscode-remote.remote-ssh,ms-vscode-remote.remote-ssh-edit config file
This is because the config file cannot be accessed normally. We can create a new config file (this file needs to be accessible normally), such as D:/.ssh/config, and then specify the configuration file through the -F option: ssh -F D:/.ssh/config username#ip_address -p port
Delete the .config file, it has worked for me

git "fatal: .git/index: index file open failed: Permission denied"

So, I have been working on a project and have been using git to push to github, and then suddenly today after I have been working for a little while I went to git add and got fatal: .git/index: index file open failed: Permission denied
I can't think of anything I have done recently on my computer that would cause this. Although I woke up today noticing my computer had rebooted overnight, and I noticed some different settings were not how I normally leave them. For instance quiet mode was turned off, and I always have that on. Thought it was kind of weird but ran a virus scan and didn't notice anything so I shrugged it off thinking it may be something on window's end.
Should I be nervous something is playing with my permissions? After a few minutes of googling I haven't found out too much on this error. Kinda baffled because I haven't personally changed any permissions and it everything seemed fine yesterday.
Looking for some help, if there is anything else I need to include to solve this error I'll gladly provide it if asked. Nobody else uses this computer. It is a Windows 10 build, git version 2.6.3.windows.1.
edit: Here's the output
11:21:34.673917 trace.c:319 setup: git_dir: .git
11:21:34.674417 trace.c:320 setup: git_common_dir: .git
11:21:34.674417 trace.c:321 setup: worktree: D:/Downloads/AnotherTabExample
11:21:34.674417 trace.c:322 setup: cwd: D:/Downloads/AnotherTabExample
11:21:34.674417 trace.c:323 setup: prefix: (null)
11:21:34.674417 git.c:348 trace: built-in: git 'add' '.'
fatal: .git/index: index file open failed: Permission denied
Mark: If you have this error in Android studio, just restart your android studio and this error will be solved. I guess that This is because of some 'cache' or 'bug' of intellij idea.
From the verbose output, it appears that the permissions of the .git/index while might have gotten corrupted.
Confirm that this is the case by doing a ls -al .git/index followed by a chmod 644 .git/index to fix it. Then try running the git add command again.
Update
It turned out to be a (Windows) permissions issue. What worked was simply creating a copy from within msysgit of the original folder and using that (i.e. cp -rvf original original-bkup).
A reset of the permissions on the original folder should have worked, but didn't.

Error on git pull error : cannot open .git/FETCH_HEAD: Permission denied

I'm looking for help !!
I am getting the following error message when trying to complete a git pull;
C:\Jenkins\Repo> git pull error: cannot open .git/FETCH_HEAD: Permission denied
The machine in question is Windows Server 2008 r2 OS and were using ssh to handle the authentication.
We have tried the following;
Checked that the current user the correct read/write on the FETCH_HEAD file, which it does, also checking that the user has the correct permissions set on the repo root.
I have tried to load my private key (which I know 100% works and has permissions to the repo in question) and still the same issue... so from little I know regarding git I think this is more of a Windows issue
and lots of Google !
Any more ideas on what to do/check would be a great help !
This happened to me after I upgraded to Windows 10. While my user is an administrator and Administrators had full access to the root repo folder, my user was not explicitly listed. I've added my user with Full Control and it solved the problem for me (had the same issue with Outlook refusing to read the PST file until I did the same thing).
So, for me, the solution is:
Right click on the folder, select properties
Security
Edit
Add
Type in my user name
Check Full Control under Allow
OK, OK
On Windows 10 this is what worked for me:
1. go to the repo folder
2. right click on the .git folder and choose the last option - properties
3. on the general tab uncheck hidden checkbox if checked
4. hit apply and then ok
now go try git fetch or git pull and it should work.
This is fairly a common problem. I've come across it many times and almost all of the times, the issue is with the right permissions to the repo/directory .git/ and the right SSH keys to access the git repository.
You probably need to make the user, the owner of the repository chown (Give full access to the user) or, clone the repository to a different directory.
You can set the write permission with the following command
go to your folder chown -R youruser:yourgroup .git/
Also try to un-hide the .git folder.
In my case, this happend because I hide the .git folder by hand(usually it will be hide automatically) but I forgot it.
I have tried edit security but no effect. So I just show the .git folder and solve the problem.
May this can be help for someone
This happened to me because after updating windows.
Kindly try these steps:
Right click on folder -> properties
Under general there are two checkbox hidden and read only -> uncheck the hidden check box and click on apply
Under security -> edit -> add user -> apply
give full access to your user
This will work for you
just remove the folder,and clone again.
What I did was open powershell / command prompt with Administrative rights inside the repository, and I was able to pull / fetch / merge and push.
This problem can also be caused by the caches Jenkins keeps of it's Git operations. I had tried chowning the files I thought were causing the problem, I also deleted the workspace completely.
After deleting %ProgramData%\Jenkins\.jenkins\workspaces\MY-BUILD* I still had the exact same error message.
In %ProgramData%\Jenkins\.jenkins\caches I deleted everything but you could probably just delete the git-<HEX_ID> and git-<HEX_ID>#tmp folders and retry the same checkout. This resolved the issue for me as Jenkins was forced to recreate the .git folder in both the workspace and the cache and the permissions were then correct.
Steps:
Delete the workspace folders of the troublesome job
Delete the Git caches
Retry the job

Git rename from index.lock to index failed

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'.
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.
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
In my case I had to close the VS code which I opened with code . from a WSL Ubuntu terminal.
It can be a real issue, try to run your Terminal as Administrator instead of user. Worked for me
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.
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.
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
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.
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.
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"
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.
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
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.
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
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).
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.
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)
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.
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.
Right-click on .git, and open Properties.
On the Security tab, click Advanced.
In Advanced Security Settings, next to Owner, click Change (even if it's already correct).
In Select User, select the regular user, and click Okay.
On the Security tab, under Owner, you now have a Replace owner of subcontainers and objects checkbox - Check this and click Okay.
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).
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.
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
Restart your Source Code editor; killing a Process may work but is not polite_
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.
I change project permission
Then, I exit Code Editor

Resources