git rebase: "error: cannot stat 'file': Permission denied"
Solution 1
I've only ever seen this error on Windows and what it seems to mean is that something blocked git from modifying a file at the moment when it tried to a apply a patch.
Windows tends to give processes exclusive access to files when it shouldn't really be necessary, in the past virus checkers have been one source of suspicion but I've never proved this conclusively.
Probably the easiest thing to do is to abort and try again, hoping that it doesn't happen the next time.
git rebase --abort
You can attempt to use git apply
and knowledge of what commit git was actually trying to do before doing a git rebase --continue
but in all honesty I wouldn't recommend this. Most of the times I've seen this tried there's been a better than evens chance that something gets accidentally missed or messed up.
Solution 2
Try closing any programs that have the folder open, such as editors, explorer windows, command prompts, and FTP programs. This always fixes the issue for me on Windows.
Solution 3
Just close your IDE (VISUAL STUDIO/ATOM etc). It might work
Solution 4
When I see this on my machine, it's worse than just a "some process has the file open". The actual ownership of the file gets jacked up to the point where I (running as administrator) can only access it after rebooting.
Nearest I can tell, IIS is part of the problem. If I switch between two major branches that require a lot of files to modify, git will delete a file or directory (usually DLLs) while IIS is trying to do something or another with it. At this point, the IIS process automatically overwrites the file on disk with a version that's locked and appears to be owned by nobody.
Stopping IIS at this point doesn't do it. Best I've found out to do is to reboot, and remember to stop IIS before changing across major branches in the future.
I know that doesn't really answer the question, but might be helpful to others.
Solution 5
if using vscode, kill terminal and open new one. else maybe close terminal too
![andriy](https://i.stack.imgur.com/8Q4Iy.png?s=256&g=1)
andriy
A .NET developer in the Germany/France/Switzerland border area. I do web development, desktop development, Selenium automation, and lots more. Currently employed at Dreamlines GmbH. I can help you transition your team to Git automate your regression tests release your next great ASP.NET MVC, WPF, or even WinForms app chase down those hard-to-catch bugs write tests for things that are hard to test I speak fluent English, excellent Romanian, and pretty good German. And a smattering of French, at least enough to tell the airport taxi drivers how to find my house.
Updated on July 08, 2022Comments
-
andriy almost 2 years
I'm using git, and made a small commit followed by a large one. I decided to use
git rebase
to squash the two commits together before pushing them. (I've never done this before.)So I did:
git rebase -i HEAD~2
This gave me my editor, where I chose to pick the earlier commit and squash the later one. When I saved, git said:
error: cannot stat 'filename': Permission denied
Could not apply sha1 for later commit... initial line of text for that commit
Now:
- Neither commit appears when I do
git log
. git status
tells me I'm "Not currently on any branch."- One file is listed as modified and in the index, and two files are listed as untracked. My first commit had just one file (I think), and my second commit had a good dozen.
What happened!? How do I fix it?
- Neither commit appears when I do
-
andriy about 13 yearsWhen I try to abort, I get "error: unable to create file same file it couldn't stat (Permission denied)" and then "fatal: Could not reset index file to revision 'big long sha1'.
-
andriy about 13 yearsAlthough...it does look as though my commits are back, which is something I was worried might not happen.
-
CB Bailey about 13 years@Kyralessa: Hmmm, have you tried rebooting? If something is persistently locking that file then after a reboot (or something slightly less drastic that releases the file) you should be able to
git checkout -- previously-locked-file
and be back up and running. -
andriy about 13 yearsWell, I'm still not sure exactly what happened, but as best I can tell, VS 2010 was locking the file (odd because it was a .xml doc file). Process Explorer didn't find anything locking that file, but after I exited VS, I was able to use
git checkout
to get the file back (ingit status
it was deleted), and now everything is back as it was before I tried to rebase/squash. Perhaps I should try again, though I feel a bit queasy at this point. -
andriy almost 13 yearsFollow-up, many moons later: I haven't had this problem again. Rebasing has worked fine, including even interactive rebasing. It must've been a momentary VS file-locking glitch.
-
andriy almost 13 yearsGood to know, though in my case I'm not using TortoiseGit; I just use the command line.
-
Ethan Brown about 12 yearsHi, Mike...we're having this exact same problem, but it came out of the blue. We've been using the same process on many projects with IIS running, and never had a problem. One day, though, it starts happening...baffling and frustrating. Have you learned any more since you wrote this?
-
Jerry about 12 yearsRan into this on a merge. Apparently Gimp had a hold on a file which I had already closed. Closed Gimp itself and all's well.
-
jacobsimeon about 12 yearsI was getting the same error. I just closed visual studio and everything worked.
-
Larry about 12 yearshad a similar issue. Has happened twice since renaming folders in my development branch. Exiting my editor fixes the issue.
-
Trev about 12 yearsClosing Visual Studio 2010 fixed it for me too
-
Michael Martin-Smucker about 12 yearsI closed just about everything I could think of before I remembered the WinLess LESS -> CSS converter hiding in the background.
-
Ates Goral over 11 yearsIt was Github's Windows client for me. Closing it fixed the problem. Thanks!
-
Eponymous over 11 yearsThis problem also occurs in Windows if Matlab has a directory in its path. (I encountered the problem after a git checkout where a directory on Matlab's path changed.)
-
CodeHxr over 11 yearsVisual Studio held a lock on a nuget package when I tried to merge. Closing VS worked for me.
-
longda over 11 yearsMost common source of this error, that I've seen on Windows, is Visual Studio locking up the files. As prescribed, close VS before switching branches, merging, etc.
-
Scott Tesler about 11 yearsCameron White's answer should be the accepted answer - it's the better solution.
-
CB Bailey about 11 years@ScottDavidTesler: It only addresses the initial cause; it doesn't address how to recover from the aborted state that Git left behind.
-
shovavnik almost 11 yearsIn my case, it was avast's Script Shield or Behavior Shield (not sure which, but definitely one of those two). Stopping them briefly solved the problem.
-
d48 almost 10 yearsMy issue was Visual Studio had a lock on the file. This should be the accepted answer
-
nickb over 9 yearsThis should definitely be the accepted answer. Rebooted, everything worked. Could be either the editor (Sublime in this case), IIS, or SASS preprocessor, but something had files in use.
-
tekiegirl over 9 yearsIt was Visual Studio for me. No problems before, but it decided to hold onto a package file for me this time.
-
Steven over 9 yearsSome things I've tried that may work: 1. close all programs that may have be reading the files in question 2. perform several smaller rebases instead of one big one. Sometimes Git seems to mess up and this approach may work
-
hatef about 9 yearsWorked for me too - I stopped Apache and MySQL in XAMPP before pulling.
-
fundead about 9 yearsThanks for the insight Mike - in my case checking out an old branch with an ASP.NET MVC project which had a different binding URL to the previous branch caused Visual Studio then IIS to hold a lock on some files in the project. Stopping the appropriate apppool in IIS removed the lock.
-
Kiran Kotla about 9 yearsI don't think it's Windows giving processes exclusive access when it's not necessary, but rather processes asking for it when they don't need it.
-
ajm almost 9 years"Me too" answer for Atom. It seems to lock files and directories; closing it fixes the issue.
-
Hal over 8 yearsAlso got it with Atom just by having the project/file tree open
-
Nikhil Kuriakose almost 8 yearsThank You! Not everyday will you see the 'right' answer down the list. I recommend this to be marked the right answer. Rather than aborting the current operation, makes sense to solve the real problem.
-
Ali Haider almost 8 yearsFor those who have done the new keygen generation and they still get an error if they try to push/checkout a branch etc, then try to close your IDE (Atom/VS etc). After scratching my head for an hour, I found out that my old key was perfectly fine and Atom IDE was the culprit. I simply restarted it and my problem was solved.
-
imbatman almost 8 yearsDon't really need to run the rebase --abort
-
Daniel Lizik almost 8 yearsHad a server running in a shell file, closing this fixed it for me, thanks.
-
jsalwen almost 8 yearsLike @Hal, my issue was with Atom. It was because a folder in one branch was not in another branch. Closing Atom will fix it, but you can also collapse the project tree (to hide the folder), and that works too.
-
alexm over 7 yearsIIS was the culprit for me as well; when this pops up a quick
iisreset
seems to release the file locks. -
Phil Carter about 7 yearsIn IntelliJ, stopping the running Tomcat service worked for me. Slightly easier than restarting IDE.
-
frostymarvelous almost 7 yearso man! I was running elm-live in the background. This saved me a ton of headache.
-
CharlieB over 6 yearsI've found that the same thing happens with OneDrive: switching branches in a repo stored in OneDrive really confuses it
-
Todd over 6 yearsGit will often dump the changes into the working directory when it fails to change a branch due to locked files. I often find myself performing
git reset --hard
(remove working dir changes) followed bygit clean -fd
(remove untracked files and directories) -
Augustin Bralley over 6 yearsAtom also apparently locks files. Restarting fixed it.
-
Pieterjan Spoelders about 6 yearsI rebooted which amounts to closing your IDE ;).. also fixed it for me!
-
lancemonotone about 6 yearsThis was the case with me, using PhpStorm. Restarting it fixed the error.
-
Vince I over 5 yearsI also had a Git Bash window open - closing that worked for me.
-
qdread over 5 yearsThis also happened to me on win10, requiring reboot but I didn't test the
iisreset
solution. -
PolarisTLX about 5 yearsClosing Atom editor solved this for me on Windows 10
-
mfru almost 5 yearsIn my case it was a PDF viewer that was blocking a file inside the folder git was trying to access.
-
Edwin Pratt almost 5 yearsI found that if Cygwin is open in a folder (like
tests
), and I want to merge a branch into master, I have tocd ..
to the root directory and then perform the merge in Git Bash. -
LightCC almost 5 yearsAny IDE worth it's salt should not be locking anything in a repo... thumbs up!!
-
Mike Upjohn almost 5 yearsThis was it, looked like a process locking or something
-
Chrysus over 4 yearsFor me, this was due to running the relevant application in Windows' Ubuntu terminal while switching branches in Git Bash. Normally, file changes are fine while the app is running due to live reload. However, it seems that when switching branches that would normally cause a folder deletion while something else has a lock on it, the ownership on the folder gets totally obliterated and the Windows user can't do anything. The folder that would have been deleted couldn't even be accessed, preventing any other action in Git that might involve that folder. Took me far too long to figure that out.
-
mickburkejnr about 4 yearsHit this issue after checking out in to a temporary branch using Atom on Windows 10. Closing and re-opening Atom resolved the issue for me.
-
Fenio about 4 yearsAlso helped in IntelliJ
-
Francesco Dassisis almost 4 yearsfatal: No rebase in progress?
-
Shilpa almost 4 yearsI kept closing apps I felt relevant and it got resolved
-
8znr almost 4 yearsThe same for me, closing VS2017 worked. I had a Folder Project open (Linux CMake)
-
Jean Libera over 3 yearsI got this error message with Visual Studio 2019 open. After I closed it, the error went away.
-
user710 over 3 yearsClosing some programs worked well for me while I was so worried that I could not retrieve those files. Thank you so much!
-
jaimedash about 3 yearsFor me, a windows administrator permissions window had popped up in the background. Checking allow fixed the error
-
GoFindTruth about 3 yearsstill find this useful in 2021!
-
CervEd about 3 yearsI don't understand the recommendation to abort. AFAIK if the pick fails, it jsut get's rescheduled. Restore the working space and then
git rebase --continue
-
shutsman almost 3 yearsReopening WebStorm helped me