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?
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 andRemove-Item
refuse to delete it, unless you specify-Force
parameter. – user364455 – 2018-05-13T17:48:06.893I 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
– user364455 – 2018-05-17T07:44:32.873.git
directory? Also you did not show the error message, which you get, when you use Command Prompt instead of PowerShell.@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 youdel /s
). Usermdir /s
instead. – Bob – 2018-05-18T01:59:12.583Does
del foo -Recurse -Force
fail with same error? And as @Bob say, you need to userd /s
in Command Prompt to remove directory. – user364455 – 2018-05-18T06:18:02.300