49

On my Windows machine, when I pull my repo I get:

error: unable to crate file <path to file> (Permission denied)

I think the problem might be that when I look at the properties of the folder there is a square inside the checkbox, I clear it out so that no files or folders will be read only.

After pressing ok and checking to see if Windows applied my changes, I see the square again!

How can I get Git or Windows to allow me to get my commits?

rp.
  • 3,435
  • 1
  • 21
  • 29
  • Sometime there's no way to determine what files windows keep open because windows doesn't provide an lsof. So one way it to restart Windows. – Owl Dec 07 '16 at 14:48

8 Answers8

55

Run the Git Bash or the console you are running, with Run As Administrator

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • I had composer.phar installed in my home directory on C. This worked to run: ```bash composer.phar self-update ``` – b01 Aug 16 '14 at 10:36
  • 2
    Make sure you don't have other apps that have the files on your project open. – Leandro Aug 05 '15 at 20:37
  • Interesting - this turned out to be the problem. I already had the command window open as administrator, but for some reason it was no longer authorized. I closed and reopened the command prompt as administrator again and it worked. – NightOwl888 Aug 16 '16 at 04:06
  • 12
    I give this answer a downvote in favor of Volodymyr's answer, because you shouldn't have to run git commands as administrator for many reasons!! Things are set up wrong if you have to do it this way. In my case closing a few open windows solved the issue, as suggested by Volodymyr. – Dominic Jan 04 '17 at 11:27
  • 3
    Please explain why you should "run as administrator" – Daniel James Bryars Mar 03 '20 at 09:25
  • the better option is to `chown -R $USER `. Don't throw "run as administrator" or sudo to every problem. You'll only write more admin privileged files to disk and make your problem worse – MuhsinFatih Jun 03 '21 at 21:31
35

Check if another application is using your files and kill it. In my case it was grunt, always watching for the changes and building project. Gave me the same message as yours, when discarding files.

How it happened? When I've switched to another branch, some non-existent files appeared in my working copy. I could not Discard them, nor open in the text editor.

Volodymyr Kotylo
  • 483
  • 5
  • 11
  • 3
    Sublime Text caused the issue here. You can use Microsoft Process Monitor (google it) filtered by path to see what process is bugging with the files during your checkout. – SzieberthAdam Mar 29 '16 at 12:11
  • 1
    In my case the Visual Studio Code caused the issue. After stopping it everything worked fine. – Zoltan Szabo Sep 07 '20 at 06:28
  • Using Windows 10, I had an Explorer window open on a second monitor... – schande Nov 16 '20 at 09:55
21

In my situation with the same problem it was another program ( Atom ) which blocked files. Closing it helped.

So check if any other program, code editor is using the same files, if yes - turn it off.

Maciej Sikora
  • 19,374
  • 4
  • 49
  • 50
  • How did you get that? Is there any command that shows master files are blocked by any process? – aquaman Sep 24 '17 at 14:50
  • Same here- The reason I know it was Atom was because I closed all other apps and the problem wasn't resolved until I also closed Atom. I believe that Atom does something unexpected to the file system on PC that caused the issue. – jguffey Mar 23 '18 at 21:40
3

Adding this as for me none of the above answers worked: if you have it installed, check Bitdefender settings (or other security software you have). It operates with protected folders and might block any attempts to manipulate files. When using for example git bash, commands like rm, vim, etc., all count as separate ones and need separate permissions - not only bash.exe itself.

luxandras
  • 31
  • 1
2

I just had this kind of error after aborting a rebase. Turned out there was an orphaned git process that was keeping a lock on a file in .git directory.

Otherwise a script from this answer might also be useful.

Community
  • 1
  • 1
jakub.g
  • 38,512
  • 12
  • 92
  • 130
0

Git Bash on Windows 10. The file was not read-only, so I closed the bash window and opened again and it worked.

markau
  • 854
  • 8
  • 21
0

I tried all suggestions, none worked. Then i tried existing solutions again to check if any app is causing it. It was Avast antivirus !!

0

Turning off Windows UAC seemed to do the trick.

rp.
  • 3,435
  • 1
  • 21
  • 29