DAXaholic
DAXaholic

Reputation: 35338

"git clean -f" doesn't leave an empty directory

If I have a directory app in my repository with a single, untracked file in it and I execute git clean -f, I get the message: Not removing app/ and the untracked file is still there.

But if I have an additional, tracked file within the same directory and I execute the exact same command then the untracked file is removed successfully.

So my question is: Why is Git trying to delete the directory if it would be empty after cleaning it from untracked files?

Below is a script which reproduces the behavior. Thanks in advance.

#!/bin/bash

# Create new empty repo
mkdir myrepo && cd myrepo
git init

# Create app directory with main.c
mkdir app
touch app/main.c

# Try to delete main.c with git clean -> Not working
git clean -f

# Add helper.c to app directory and add to index
touch app/helper.c
git add app/helper.c

# Try to delete main.c with git clean -> Now it's working
git clean -f

Upvotes: 5

Views: 843

Answers (2)

Anshul Goyal
Anshul Goyal

Reputation: 76837

git does not keep track of directories - you can't commit an empty directory to git.

(try creating an empty directory, and adding it to git - you won't see any changes in git status)

Even if you add some files to an untracked directory, git won't show you all the files in git status - it will just show the directory

Check this question for more answers on why git doesn't version directories.

So as @Hasturkun pointed out in his answer, you need to pass -d to git clean to remove untracked directories

Upvotes: 2

Hasturkun
Hasturkun

Reputation: 36402

You need to pass the -d option for git clean to remove untracked directories.

Upvotes: 6

Related Questions