Why does Git for Windows create repositories that I can't delete from a command line?

7

2

I have freshly "Reset" installation of Windows 10. I installed the "Git for Windows" distribution of Git from https://git-scm.org/.

If I create a git repository in any way (whether init or clone), that repository can't be deleted from my file system with a command line prompt. I have to use Windows Explorer to do it.

Here's a sample session:

PS C:\Users\radix> mkdir foo


    Directory: C:\Users\radix


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        5/10/2018   5:46 PM                foo


PS C:\Users\radix> cd foo
PS C:\Users\radix\foo> git init .
Initialized empty Git repository in C:/Users/radix/foo/.git/
PS C:\Users\radix\foo> cd ..
PS C:\Users\radix> rm foo -Recurse
rm : Cannot remove item C:\Users\radix\foo\.git: You do not have sufficient access rights to perform this operation.
At line:1 char:1
+ rm foo -Recurse
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (.git:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
rm : Directory C:\Users\radix\foo cannot be removed because it is not empty.
At line:1 char:1
+ rm foo -Recurse
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\radix\foo:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

This is PowerShell running as my normal user. Even if I run Powershell as an administrator, the rm -Recurse command still fails in the exact same way. The only way I know of to delete this new foo directory is to do it via Windows Explorer, which doesn't even complain or prompt about permissions - it just silently deletes it with no problem.

I don't believe this is a PowerShell-specific issue, because a similar behavior happens in CMD:

C:\Users\radix>dir foo
 Volume in drive C is Blade
 Volume Serial Number is B83C-EF1B

 Directory of C:\Users\radix

File Not Found

C:\Users\radix>git init foo
Initialized empty Git repository in C:/Users/radix/foo/.git/

C:\Users\radix>del foo
C:\Users\radix\foo\*, Are you sure (Y/N)? y

C:\Users\radix>dir foo
 Volume in drive C is Blade
 Volume Serial Number is B83C-EF1B

 Directory of C:\Users\radix\foo

05/17/2018  08:46 PM    <DIR>          .
05/17/2018  08:46 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  275,911,991,296 bytes free

C:\Users\radix>dir /a foo
 Volume in drive C is Blade
 Volume Serial Number is B83C-EF1B

 Directory of C:\Users\radix\foo

05/17/2018  08:46 PM    <DIR>          .
05/17/2018  08:46 PM    <DIR>          ..
05/17/2018  08:46 PM    <DIR>          .git
               0 File(s)              0 bytes
               3 Dir(s)  275,866,763,264 bytes free

The DEL command does not display any errors, but it fails to actually delete the repository.

It's important to note that the rm command does delete the files from the repository, including the files inside the .git directory. But it leaves the .git directory there.

It has been posited that the "hidden" nature of the .git subdirectory is causing a problem. Here is a transcript of my experimentation regarding that:

PS C:\Users\radix> git init foo
Initialized empty Git repository in C:/Users/radix/foo/.git/

At this point, I open Windows Explorer and navigate to the C:/Users/radix/foo directory, and rename .git to git. I also right click on the .git directory and open the Properties dialog, noting that it does not have the "hidden" flag. I also navigate into the git directory and right-click-open-properties on several subdirectories and files, noting that none of them have the "Hidden" flag set.

And then I try to delete the directory from powershell:

PS C:\Users\radix> del foo

Confirm
The item at C:\Users\radix\foo has children and the Recurse parameter was not specified. If you continue, all children
will be removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): a
del : Cannot remove item C:\Users\radix\foo\git: You do not have sufficient access rights to perform this operation.
At line:1 char:1
+ del foo
+ ~~~~~~~
    + CategoryInfo          : PermissionDenied: (git:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : RemoveFileSystemItemUnAuthorizedAccess,Microsoft.PowerShell.Commands.RemoveItemCommand
del : Directory C:\Users\radix\foo cannot be removed because it is not empty.
At line:1 char:1
+ del foo
+ ~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\radix\foo:DirectoryInfo) [Remove-Item], IOException
    + FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand

I do notice that several of the folders in this hierarchy are marked as "Read-Only". Is that related?

So I have two questions: why can't I delete this folder, and why is git creating it in a way that it can't be deleted in the first place? If the problem is that the directories are read-only, why is git creating them as read-only?

Christopher Armstrong

Posted 2018-05-10T22:55:03.070

Reputation: 123

Which error did you get when you are deleting directory from Command Prompt (CMD)? While I get the same error when deleting from PowerShell, I do not get any error when deleting it from CMD. – user364455 – 2018-05-13T17:40:59.113

Further investigation show that issue is caused by the fact that .git directory is hidden and Remove-Item refuse to delete it, unless you specify -Force parameter. – user364455 – 2018-05-13T17:48:06.893

I do not believe that the hidden nature of .git is affecting things at all. When I remove the "Hidden" attribute from .git (and all items recursively), and rename it to "git", the behavior is the same. – Christopher Armstrong – 2018-05-14T13:34:34.037

Did you try another folder except C:\User\user_name? Also try with git in cygwin (i.e. install cygwin, open cygwin setup, install git). – Biswapriyo – 2018-05-15T15:15:57.287

@Biswapriyo my question is specifically and intentionally about Git for Windows. and yes, I've tried it in directories like ~/Documents and subdirectories of ~/AppData/Local. – Christopher Armstrong – 2018-05-16T18:22:57.240

I tried latest git-for-windows in my PC and it works well. I didn't see your issue. – Biswapriyo – 2018-05-16T20:24:39.357

@ChristopherArmstrong It does not matter if you believe this or not, but it is here in the source. And this is the only place, I can find, which can generate particular error message. So, does the error message change after you unhide the .git directory? Also you did not show the error message, which you get, when you use Command Prompt instead of PowerShell.

– user364455 – 2018-05-17T07:44:32.873

@PetSerAl hey, sorry about the lack of updates, I will edit the question with more details shortly. – Christopher Armstrong – 2018-05-18T01:47:31.527

You're using the wrong command in cmd... del only deletes files (and even then not recursively unless you del /s). Use rmdir /s instead. – Bob – 2018-05-18T01:59:12.583

Does del foo -Recurse -Force fail with same error? And as @Bob say, you need to use rd /s in Command Prompt to remove directory. – user364455 – 2018-05-18T06:18:02.300

Answers

8

I'll start by addressing the easier case: del in cmd will never do what you want, because it only removes files. Even then, without /s it won't recurse and will only remove files in the top-level directory.

So, for cmd, you should be using rmdir /s to recursively delete all files and folders. Add /q if you want to disable the confirmation prompt.


Now, PowerShell. Remove-Item (which is what rm and del alias to) with just -Recurse will not recurse into hidden directories, and will therefore not delete their contents. Which leads to your error - which is not a "permission denied" but rather a "directory not empty".

To get around this, you can pass the -Force parameter (equivalent to *nix -f). So rm -r -force or Remove-Item -Recurse -Force should work.

I'm not entirely sure how you're getting a non-hidden .git directory - GfW (admittedly an older 2.9.2) creates them as hidden for me. In cmd, run attrib .git to check if it's hidden. Once un-hidden, a Remove-Item -Recurse works without -Force.

Bob

Posted 2018-05-10T22:55:03.070

Reputation: 51 526

1Git for Windows is creating .git as hidden. In one of my transcripts I described how renaming it to git and ensuring it is non-hidden is not solving the problem in powershell. I added that section in response to some comments on my original question. – Christopher Armstrong – 2018-05-18T13:49:11.173

@ChristopherArmstrong Works for me after un-setting hidden... try again with just Remove-Item -Recurse. And try the -Force option without unsetting hidden. – Bob – 2018-05-18T14:31:45.687

I found the problem: it's not just that the .git directory has the H attribute, but also that the pack files in .git/objects/pack have the R (read-only) attribute. Once I remove those attributes as well, I can finally delete the git repo without passing -Force. – Christopher Armstrong – 2019-04-17T14:50:32.953