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

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?


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.