threads / discuss / 41340

git clean without ignored files

Subject: git clean without ignored files

## tl;dr

One message between Feb 5, 2016 and Feb 5, 2016.

replies: 0people: 1as markdown or json

Robert Dailey· Feb 5, 2016, 15:42 UTC · lore

I noticed that `git clean` does not handle a specific scenario. I have the following types of untracked entities in my working copy:

* Untracked files in tracked directories (non-recursive; sibling files
are tracked)
* Untracked files in untracked directories (recursive)
* Ignored files meeting the same criteria above

My goal is to clean the first 2 points, but not the 3rd. Basically, I want all untracked files that show up in `git status` to be removed, but not ignored files or directories.

If I run this command:
$ git clean -nd

I get a list of all the untracked files but oddly I get a few directories in the list that indirectly ignored via my .gitignore. Basically, the directory itself isn't ignored by any pattern in my .gitignore but its entire contents (recursively) is ignored, due to patterns matching the files themselves.

I think `clean` sees this as an untracked directory, because it's not specifically matching a pattern in my .gitignore. Whereas git status is smart enough to not display it because it knows that every single file contained in there, regardless of depth, matches an ignore pattern.

As a workaround, the following command accomplishes what I want:
$ git ls-files --others --exclude-standard | sed 's/.*/"&"/' | xargs rm -rfv

Is there a reason why `git clean -nd` file listing doesn't match more closely with what I see in `git status` regarding untracked files and directories?

Note I am aware git does not directly track directories. So when I refer to tracked/untracked directories, I'm actually referring to that directories contents recursively. If recursively 100 files exist, but only 99 are ignored, then I consider that root directory to be "untracked" but not ignored, because 1 file still shows up in `git status`.

← back to recent threads