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

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.

Related

Git Gui on Windows: couldn't open ".git/PREPARE_COMMIT_MSG": permission denied

Been using Git Gui on Windows on a work computer for over two years now. Last night the repo was working fine, but this morning after a reboot, Git Gui pops up a message about "permission denied".
No idea what changed, although we did get an email saying that a corporate antivirus update had been pushed out which required a reboot. Also, a Windows update for December 2022 was installed on my machine which also required a reboot.
The full error message is:
couldn't open ".git/PREPARE_COMMIT_MSG": permission denied
couldn't open ".git/PREPARE_COMMIT_MSG": permission denied
while executing
"open [gitdir PREPARE_COMMIT_MSG] a"
(procedure "run_prepare_commit_msg_hook" line 8)
invoked from within
"run_prepare_commit_msg_hook"
(procedure "rescan" line 27)
invoked from within
"rescan ui_ready"
(procedure "do_rescan" line 2)
invoked from within
"do_rescan"
("after" script)
Things I have tried:
Based on suggestions in threads with similar "permission denied" errors, I ran the follow from a command prompt:
icacls "path\to\Work.git" /grant MyUserName:(OI)(CI)F /T
The command executed without errors, but no change to the repo.
Ran Git Bash (MINGW64) which runs without any immediate errors but only git status works. git add gives the error fatal: Unable to create '/path/to/Work/.git/index.lock': Permission denied
Tried a separate, personal, repo on the same work computer for a repo hosted on github.com. That worked fine (can commit, pull, etc.).
Any thoughts? My user account is part of the Administrators group, but it looks like the Windows and "corporate anti-virus" updates have modified or blocked something to do with my user account.
UPDATE 1
Rebooted Windows into Safe Mode. Now I can git add and git commit, though git push fails due to a failure to resolve dev.azure.com (something to do with the network not loaded in Safe Mode). Rebooted back into normal Windows and the problem returns: git add and other commands fail immediately and git push finds dev.azure.com but I get a permission denied error.
UPDATE 2
Based on the comments from #LeGEC, I checked the repo .git folder. There is no PREPRE_COMMIT_MESSAGE or index.lock file. The permissions haven't been touched by me, but after explicitly allowing my user "Full Control" over the folder, its still not working - still "permission denied" errors.
A colleague does not use Git for Windows, and was not having issues after the recent update. He suggested I try Visual Studio (which is tied directly into dev.azure.com)...and git works from Visual Studio 2019!
So we think that recent anti-virus or group policy changes has revoked, blocked or broken authentication of my user (presumably) connecting to the corporate network's and/or dev.azure.com. Somehow Visual Studio is able to authenticate me (through the Mcicrosoft Azure account?) which fails when using my Windows account....does that make sense?
Will update this question as and when I find out more details.
If anyone has seen this type of thing before (suddenly one application stops working with git with "permission denied" errors, while another works), your input would be appreciated!

Git installation failed [duplicate]

I am new here and i will try to explain my question kindly ignore any mistakes.
I am using git version git-2.8.2
It worked fine for one day then this problem occurs.
I am using gcloud repository.
First I tried gcloud clone command then this error occurs
Then to make sure git is there I tired git command then this error occurs
Then I double check by opening git Bash but same error was there too.
I tried reinstalling changing directory. but nothing works.
I face the same problem after I try to avoid memory leak in Windows 10. If you happened to change the regedit like me, just type regedit in the search then go to
HKEY_LOCAL_MACHINE -> SYSTEM -> ControlSet001 -> Services -> Null
change the value of Start to 1.
I accidentally bumped into the same problem when I was sorting out the services running on my computer with Windows 10.
fatal: open /dev/null or dup failed: No such file or directory
The reason was that I deleted the service named 'Null' that had no description as I thought that was a virus service.
Thus, when I found my git unable to operate, I reckoned the deleted service.
According to a solution provided on some site I tried to run the service again using cmd.exe
sc config Null start= system
sc start Null
but it said the service hadn't been existed in the list.
Thankfully, there are some kind folks who shares the information of the default services running on Windows 10 and the description necessary for the successful bringing back the service.
So as to get the service back in the list:
press Win + R
type regedit
go to HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services section
Create Null folder and all the params it needs.
Restart your computer.
Now you got your Null service back and your git back as well.
Hope this helps.
I solved my problem accidentally. I would like to share it with everyone.
It was not a problem of git or gcloud or source tree.
Actually I have forcefully stooped windows update from installing which causes this problem.
Now when I install windows updates again this problem is fixed now.
Maybe this helps someone.
the similar situation in chrooted linux tree is fixable following way:
cd inside the folder where you are preparing the chroot dir, then
mount -o bind /dev dev/
then only chroot inside
I had this weird bug just now. I went back a dir and tried git init, it worked there.
I re-ran zsh and tried again in the dir it errored in originally and it worked. shrugs

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

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

Win 10 + Git - unable to create file - permission denied

This is really not a duplicate question. I tried few solutions like reset head^ , git checkout -- <file>,... but this does not solve my problem. The problem is that I can not revert the index.php file from "Changes not staged for commit" from git status because it throws me an error:
Unable to create file www/index.php: Permission denied during executing git "C:\Program Files\Git\bin\git.exe" -c
core.quotepath=false checkout HEAD -- www/index.php
I dont understand what is wrong with that. I am an admnistrator, console runs as administrator.. I have all permissions to do this so what permission I need? What the hell is wrong? Help me please I am in the end.
When I did git checkout -- www/index.php this didn't work for me so My problem was happening because the antivirus had moved my file www/index.php to the virus trunk (baúl de virus)
The problem with Git error - permission denied in this case has been caused by the incriminated file was open in other process. The solution was to shut down all the apps except one console, then run git checkout -- www/index.php and everything works as excepted without any trouble.

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