#StackBounty: #windows-10 #git Why does Git for Windows create repositories that I can't delete from a command line?

Bounty: 50

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:Usersradix> mkdir foo


    Directory: C:Usersradix


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


PS C:Usersradix> cd foo
PS C:Usersradixfoo> git init .
Initialized empty Git repository in C:/Users/radix/foo/.git/
PS C:Usersradixfoo> cd ..
PS C:Usersradix> rm foo -Recurse
rm : Cannot remove item C:Usersradixfoo.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:Usersradixfoo cannot be removed because it is not empty.
At line:1 char:1
+ rm foo -Recurse
+ ~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:Usersradixfoo: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:Usersradix>dir foo
 Volume in drive C is Blade
 Volume Serial Number is B83C-EF1B

 Directory of C:Usersradix

File Not Found

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

C:Usersradix>del foo
C:Usersradixfoo*, Are you sure (Y/N)? y

C:Usersradix>dir foo
 Volume in drive C is Blade
 Volume Serial Number is B83C-EF1B

 Directory of C:Usersradixfoo

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:Usersradix>dir /a foo
 Volume in drive C is Blade
 Volume Serial Number is B83C-EF1B

 Directory of C:Usersradixfoo

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:Usersradix> 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:Usersradix> del foo

Confirm
The item at C:Usersradixfoo 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:Usersradixfoogit: 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:Usersradixfoo cannot be removed because it is not empty.
At line:1 char:1
+ del foo
+ ~~~~~~~
    + CategoryInfo          : WriteError: (C:Usersradixfoo: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?


Get this bounty!!!

Leave a Reply