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

GitGit Rebase

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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 2 - Git

Just close your IDE (VISUAL STUDIO/ATOM etc). It might work

Solution 3 - Git

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 4 - Git

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 - Git

if using vscode, kill terminal and open new one. else maybe close terminal too

Solution 6 - Git

On Windows, it can be a TortoiseGIT process that blocks those files. Open task manager and end process TGitCache.exe.

Solution 7 - Git

I just stumbled upon this thread of answers - this error is such a Bogus error.# error: cannot stat 'reddit/app/views/links': Permission denied

That's all I got - when trying to merge. I read a few of the answers and then came to the realization - all I had to do was close my code editor which happens to be Atom.

Once closing the editor - I ran "git merge" again and boom , it worked.

What a pointless error:(

Solution 8 - Git

If the IDE you use(in case you use one) might have been getting in the way as well. That's what happened to me when using QtCreator.

Solution 9 - Git

This happens to me in Windows occasionally

> error: cannot stat 'filename': Permission denied

Most often I have multiple instance of bit bash open, and one of the git bash instances is in a directory that doesn't exist in the remote branch I'm pulling from.

Closing all but one instance of git bash solves the issue for me.

Solution 10 - Git

If you're running webpack shut it down. Shut down your IDE as well. Should work fine after doing those things.

Solution 11 - Git

This can also happen when you're using SublimeText and the popup window asking you to buy the program is not closed.

Solution 12 - Git

Using SourceTree in Win 10, fixed the problem by closing Atom editor.

Error reproduce:

  1. In branch B, create a md file, using Atom edit it, save and commit.
  2. Switch to branch A, pull down new commits from server.
  3. Try Switch back, Opps, it says “error: cannot stat 'file': Permission denied”.

Solution 13 - Git

This often happens when you have preprocessing software/applications watching the project, such as Prepros or Codekit. Also, Atom and Sublime (and even Notepad++) can cause this to happen if a file in the project is currently being edited.

The easiest way around the issue is to close whatever has the project files open, merge your branches, and then re-open them to refresh it. This will also avoid any problems where the program is no longer aware of any changes that have happened, forcing you to refresh the project(s) by hand.

Solution 14 - Git

Happened to me on Windows while rebasing inside IntelliJ integrated terminal. I noticed that I had Git bash client instance running in parallel.

Closing Git bash solved the problem.

Solution 15 - Git

I had a similar problem. But it was very simple to resolve. On a Windows machine, my file explorer had a folder open that existed in one branch but not in the other I checked out. Closing the File explorer resolved the problem.

Solution 16 - Git

I have just had this under Win 7.

$ git stash pop error: cannot stat 'parentFolder/subfolder': Permission denied error: cannot stat 'parentFolder/subfolder': Permission denied

Diagnosis:

1>I went to the subfolder and it's there and I couldn't delete it !

2>Use "process explorer" -> Find -> Find handles and Dlls -> put the "subfolder" name there and search.

Result: It turns out it's XMLSpy has opened one of the xml there, close XML Spy and try stash pop again, it's working now.

Solution 17 - Git

An alternate solution rather than closing all apps that might be locking the directory as just about every other answer says to do, would be to use a utility that will unlock the files/directory without closing everything. (I hate needing to restart Visual Studio)

LockHunter is the one that I use: https://lockhunter.com/ There are likely others out there as well, but this one has worked great for me.

Solution 18 - Git

Trying to close IDE such as Sublime, VS Code, Webstorm,... and close your programs that have the folder open such as CMD, Powershell, CMDer, Terminal,... will fix the issue.

Solution 19 - Git

My encounter with this problem was caused by my editor, Intellij. As part of its internal version controls, it had gone through and locked all hidden git files. (For various reasons, I was not using the git plugin that comes with Intellij...)

So I opened a normal dos window as Administrator, changed to the directory, and executed

attrib -R /S

That removed the lock on the files and everything worked after that and I could sync my changes using the GitHub windows client.

Solution 20 - Git

I agree with the above "Close Visual Studio" answers.

However, an additional step I had to do even after I'd closed Visual Studio was to manually kill the "devenv.exe" Visual Studio process in Task Explorer. After I had done this I was able to again run in gitbash:

> git pull

and the "cannot stat filename" error disappeared. It is perhaps due to a Visual Studio extension keeping the process open for longer even after closing.

Solution 21 - Git

I've just had this problem. The thing is - if you had opened file, that was removed\replaced after rebase (you had a branch which doesn't have a this file anymore), the git-system corrupts. So i closed all opened files and then tryied to checkout on some other branch

Solution 22 - Git

Same issue on Windows 10 64 Bit, running Git Bash version 2.9.0.windows1 Using Atom as my editor.

This worked for me: I added the Git software folder (for me, this was C:\Program Files\Git) to the exclusions for Windows Defender.

After the exclusion was added, git checkout 'file' worked fine.

Solution 23 - Git

This error can also be caused by the fact that files are still "locked" because of prior git actions. It has to do with how the Windows filesystem layer works. I once read a nice explanation on this, but I can't remember where.

In that case however, since it is basically a race condition, all you have to do is continue your interrupted rebase process. Unfortunately this happens to me all the time, so I wrote this little dangerous helper to keep my rebases going:

#!/bin/sh

set -e

git checkout .
git clean -df
git rebase --continue

If you want to be extra sure, you can use git rebase --edit-todo to check if the next commit to be applied is really the one that failed to be applied before. Use git clean -dn to make sure you do not delete any important files.

Solution 24 - Git

Happened to me when in windows, when using photoshop: When I saved an image and then switched to a branch (leaving photoshop with the image opened) i got the git error. Close the image in photoshop and retry

Solution 25 - Git

If you have the Meld merge tool open, close that. It blocks the file overwriting.

Solution 26 - Git

Killing the w3wp.exe process related to the repository fixed this for me.

Solution 27 - Git

In my case, I had a webpack dev server running behind.

Solution 28 - Git

I got this error when my VS1013 was on a branch targeting 8.1 and I was trying to checkout a 8.0 branch. I needed to tab back to VS and allow it to UpdateAll. Then I could checkout the 8.0 branch without error.

Solution 29 - Git

I was also on a Windows machine using Git Shell when I encountered the same error.

However, at the time I had multiple Git terminals open.

The first terminal received the error you posted about above and the other terminal had previously ran the grunt serve terminal command from yeoman (linked below). The second terminal needed to remain open to host a local server instance.

Shutting down all terminal windows running ongoing processes can cause the error to go away.

At least that's what worked for me. After I shut down the second terminal window, I could easily checkout different branches and manipulate files.

Grunt Serve Command - Yeoman.I/O
http://yeoman.io/learning/

Solution 30 - Git

I just ran into this issue. Non of the answers here happened to solve this for me.

Ended up being nuget packages I added on a branch that, once switched back to master branch, seemed to not exist. Once I did a merge it would say newtonsoft...xml could not stat. I would go to the file in question and open it but Windows threw an error back saying it can't find the file (even though I was looking right at it)

How I solved this was right click delete the file (which worked but I couldnt open it because windows couldnt find it???) and try to merge again and it solved the problem.

Very strange.

Hope this helps someone later.

Solution 31 - Git

I exited from my text editor that was accessing the project directories, then tried merging to the master branch and it worked.

Solution 32 - Git

Same problem but using SourceTree (or any other git client). I'm adding my answer as none of the answers correspond to my case.

Changing the branch from "develop" to "main" changes the actual files and subfolders of your local folder. It can happen that a folder that didn't exist in the "master" are not completely erased and windows believe you only lost your access rights (even if you're the admin). When merging from main to develop, the git client tries to access the folder. Without access rights, it returns the mentioned error.

  • Switching from one branch to the latest can fix the problem, and then back to master (double check if the folders/files are actually locally deleted).
  • Closing the client and/or your editor does not fix the problem!
  • Reboot helps but is a waste of time (IMHO)

Solution 33 - Git

In my case the file is a shell script (*.sh file) meant to deploy our project to a local development server, for my developers.

The shell script should work consistently and may be updated; so I tracked it in the same Git project as the code which the script is meant to deploy.

The shell script runs one executable, and then allows that executable to run; so the script is still running; so my shell still has the script open; so it's locked.

I Ctrl+C'd to kill the script (so now my local dev server is no longer accessible), now I can checkout freely.

Solution 34 - Git

In my case, Just update the git client to the latest version and solved the problem.

https://git-scm.com/downloads

Solution 35 - Git

I am on Windows 10 and I got this error but even after PC restart and opening with admin privileges didnt fix my problem.

So it seemd that my repository privileges was the problem.

Just to be sure I installed all new Windows updates, then I restarted my PC and finally I removed all unnecessary privileges.

Run powershell as admin and run:

attrib -s <path_to_your_directory> 
attrib -r -a <path_to_your_directory> 

Details about this powershell command are here: https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/attrib

Solution 36 - Git

We resolved permission issues by right-clicking sh.exe in Program Files and by setting "Run as Administrator" in the Security tab.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyan LundyView Question on Stackoverflow
Solution 1 - GitCameron WrightView Answer on Stackoverflow
Solution 2 - GitManJanView Answer on Stackoverflow
Solution 3 - GitCB BaileyView Answer on Stackoverflow
Solution 4 - GitMike RuhlinView Answer on Stackoverflow
Solution 5 - GitMuhammed MoussaView Answer on Stackoverflow
Solution 6 - GitromanlvView Answer on Stackoverflow
Solution 7 - GitimbatmanView Answer on Stackoverflow
Solution 8 - GitrgnglView Answer on Stackoverflow
Solution 9 - GitCrispy NinjaView Answer on Stackoverflow
Solution 10 - GitKennethDale1View Answer on Stackoverflow
Solution 11 - GitahnbizcadView Answer on Stackoverflow
Solution 12 - GitattoleeView Answer on Stackoverflow
Solution 13 - GitSteven VentimigliaView Answer on Stackoverflow
Solution 14 - GitLouisView Answer on Stackoverflow
Solution 15 - GitmmmdearteView Answer on Stackoverflow
Solution 16 - GitRoundPiView Answer on Stackoverflow
Solution 17 - GitHotNView Answer on Stackoverflow
Solution 18 - GitNavy FlameView Answer on Stackoverflow
Solution 19 - GitPerry TewView Answer on Stackoverflow
Solution 20 - GitladygargarView Answer on Stackoverflow
Solution 21 - GitBlondinkaBrainView Answer on Stackoverflow
Solution 22 - GitPaul SpavenView Answer on Stackoverflow
Solution 23 - GitraphinesseView Answer on Stackoverflow
Solution 24 - GitpscheitView Answer on Stackoverflow
Solution 25 - Gituser2441511View Answer on Stackoverflow
Solution 26 - GitRicView Answer on Stackoverflow
Solution 27 - GitCuriousGuyView Answer on Stackoverflow
Solution 28 - GitAleksandr GembinskiView Answer on Stackoverflow
Solution 29 - GitShawesomeView Answer on Stackoverflow
Solution 30 - GitTheNoobView Answer on Stackoverflow
Solution 31 - GitPudparKView Answer on Stackoverflow
Solution 32 - GitWliView Answer on Stackoverflow
Solution 33 - GitThe Red PeaView Answer on Stackoverflow
Solution 34 - GitsanmiantiView Answer on Stackoverflow
Solution 35 - GitssamkoView Answer on Stackoverflow
Solution 36 - Gito_oView Answer on Stackoverflow