git rebase: "error: cannot stat 'file': Permission denied"

275,525

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

Share:
275,525
andriy
Author by

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, 2022

Comments

  • andriy
    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?

  • andriy
    andriy about 13 years
    When 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
    andriy about 13 years
    Although...it does look as though my commits are back, which is something I was worried might not happen.
  • CB Bailey
    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
    andriy about 13 years
    Well, 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 (in git 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
    andriy almost 13 years
    Follow-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
    andriy almost 13 years
    Good to know, though in my case I'm not using TortoiseGit; I just use the command line.
  • Ethan Brown
    Ethan Brown about 12 years
    Hi, 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
    Jerry about 12 years
    Ran 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
    jacobsimeon about 12 years
    I was getting the same error. I just closed visual studio and everything worked.
  • Larry
    Larry about 12 years
    had a similar issue. Has happened twice since renaming folders in my development branch. Exiting my editor fixes the issue.
  • Trev
    Trev about 12 years
    Closing Visual Studio 2010 fixed it for me too
  • Michael Martin-Smucker
    Michael Martin-Smucker about 12 years
    I closed just about everything I could think of before I remembered the WinLess LESS -> CSS converter hiding in the background.
  • Ates Goral
    Ates Goral over 11 years
    It was Github's Windows client for me. Closing it fixed the problem. Thanks!
  • Eponymous
    Eponymous over 11 years
    This 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
    CodeHxr over 11 years
    Visual Studio held a lock on a nuget package when I tried to merge. Closing VS worked for me.
  • longda
    longda over 11 years
    Most 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
    Scott Tesler about 11 years
    Cameron White's answer should be the accepted answer - it's the better solution.
  • CB Bailey
    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
    shovavnik almost 11 years
    In 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
    d48 almost 10 years
    My issue was Visual Studio had a lock on the file. This should be the accepted answer
  • nickb
    nickb over 9 years
    This 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
    tekiegirl over 9 years
    It was Visual Studio for me. No problems before, but it decided to hold onto a package file for me this time.
  • Steven
    Steven over 9 years
    Some 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
    hatef about 9 years
    Worked for me too - I stopped Apache and MySQL in XAMPP before pulling.
  • fundead
    fundead about 9 years
    Thanks 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
    Kiran Kotla about 9 years
    I 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
    ajm almost 9 years
    "Me too" answer for Atom. It seems to lock files and directories; closing it fixes the issue.
  • Hal
    Hal over 8 years
    Also got it with Atom just by having the project/file tree open
  • Nikhil Kuriakose
    Nikhil Kuriakose almost 8 years
    Thank 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
    Ali Haider almost 8 years
    For 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
    imbatman almost 8 years
    Don't really need to run the rebase --abort
  • Daniel Lizik
    Daniel Lizik almost 8 years
    Had a server running in a shell file, closing this fixed it for me, thanks.
  • jsalwen
    jsalwen almost 8 years
    Like @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
    alexm over 7 years
    IIS was the culprit for me as well; when this pops up a quick iisreset seems to release the file locks.
  • Phil Carter
    Phil Carter about 7 years
    In IntelliJ, stopping the running Tomcat service worked for me. Slightly easier than restarting IDE.
  • frostymarvelous
    frostymarvelous almost 7 years
    o man! I was running elm-live in the background. This saved me a ton of headache.
  • CharlieB
    CharlieB over 6 years
    I've found that the same thing happens with OneDrive: switching branches in a repo stored in OneDrive really confuses it
  • Todd
    Todd over 6 years
    Git 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 by git clean -fd (remove untracked files and directories)
  • Augustin Bralley
    Augustin Bralley over 6 years
    Atom also apparently locks files. Restarting fixed it.
  • Pieterjan Spoelders
    Pieterjan Spoelders about 6 years
    I rebooted which amounts to closing your IDE ;).. also fixed it for me!
  • lancemonotone
    lancemonotone about 6 years
    This was the case with me, using PhpStorm. Restarting it fixed the error.
  • Vince I
    Vince I over 5 years
    I also had a Git Bash window open - closing that worked for me.
  • qdread
    qdread over 5 years
    This also happened to me on win10, requiring reboot but I didn't test the iisreset solution.
  • PolarisTLX
    PolarisTLX about 5 years
    Closing Atom editor solved this for me on Windows 10
  • mfru
    mfru almost 5 years
    In my case it was a PDF viewer that was blocking a file inside the folder git was trying to access.
  • Edwin Pratt
    Edwin Pratt almost 5 years
    I found that if Cygwin is open in a folder (like tests), and I want to merge a branch into master, I have to cd .. to the root directory and then perform the merge in Git Bash.
  • LightCC
    LightCC almost 5 years
    Any IDE worth it's salt should not be locking anything in a repo... thumbs up!!
  • Mike Upjohn
    Mike Upjohn almost 5 years
    This was it, looked like a process locking or something
  • Chrysus
    Chrysus over 4 years
    For 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
    mickburkejnr about 4 years
    Hit 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
    Fenio about 4 years
    Also helped in IntelliJ
  • Francesco Dassisis
    Francesco Dassisis almost 4 years
    fatal: No rebase in progress?
  • Shilpa
    Shilpa almost 4 years
    I kept closing apps I felt relevant and it got resolved
  • 8znr
    8znr almost 4 years
    The same for me, closing VS2017 worked. I had a Folder Project open (Linux CMake)
  • Jean Libera
    Jean Libera over 3 years
    I got this error message with Visual Studio 2019 open. After I closed it, the error went away.
  • user710
    user710 over 3 years
    Closing some programs worked well for me while I was so worried that I could not retrieve those files. Thank you so much!
  • jaimedash
    jaimedash about 3 years
    For me, a windows administrator permissions window had popped up in the background. Checking allow fixed the error
  • GoFindTruth
    GoFindTruth about 3 years
    still find this useful in 2021!
  • CervEd
    CervEd about 3 years
    I 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
    shutsman almost 3 years
    Reopening WebStorm helped me