Reputation: 44331
This is what I did:
$ git status
# I see some files *and* directories (some tracked, some not) which I really wanted to discard
$ git clean -xd
And I am surprised to see that git removes some other directories, which were not shown in git status
. After an initial schock (this was my /etc
in one of my production hosts) I realize that they were not listed by git because they were empty directories (so not a big deal, just mkdir
them - hoping to get permissions right). Which is fine, but:
Why on earth does git not consider empty directories when doing git status
, but does consider them when doing git clean
? This is very annoying (and dangerous)!
Upvotes: 3
Views: 4381
Reputation: 20118
As you already seem to know: You can't add empty directories in git (If not see the FAQ).
git status
effectivly tells you which changes you can add and commit. So, even if it would be possible for git status
to display empty directories, this would be highly confusing for the user.
Why can I see this directory but not track it?
git clean
on the other hand removes everything which isn't tracked. Using the -d
option also tells clean
to remove untracked directories.
But what defines an untracked directory?
As we learned, git doesn't track directories, it tracks files (or more specific file contents). In turn this means that an untracked director is nothing else but a directory which doesn't contain any tracked files. This applies for the build
folder you ignore with multiple hundred of files, as it does for the empty tmp
folder, you created earlier and forgot about.
If you want to make sure that git clean
doesn't remove anything you want to keep, you should use the -n
(--dry-run
) option (which is described in the documentation). This tells you about all files git clean
will remove.
To ensure that you don't forget -n
(--dry-run
) you can create an alias.
git config --global alias.status-clean "clean --dry-run"
Which you can now easily use like git status-clean -dx
to display all files which would be removed by a git clean -dx
call.
TL;DR: Don't use git status
to see what git clean -d
will remove. Use git clean -d --dry-run
.
Upvotes: 4