19

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?

10
  • 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. Commented May 13, 2018 at 17:40
  • 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. Commented May 13, 2018 at 17:48
  • 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. Commented May 14, 2018 at 13:34
  • 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). Commented May 15, 2018 at 15:15
  • @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. Commented May 16, 2018 at 18:22

1 Answer 1

14
+50

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.

3
  • 1
    Git 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. Commented May 18, 2018 at 13:49
  • @ChristopherArmstrong Works for me after un-setting hidden... try again with just Remove-Item -Recurse. And try the -Force option without unsetting hidden. Commented May 18, 2018 at 14:31
  • 3
    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. Commented Apr 17, 2019 at 14:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.