Git clean is not cleaning untracked files

21.5k views Asked by At

I'm getting the "The following untracked working tree files would be overwritten by checkout...please move or remove them before you switch branches" error.

The common fix appears to be git clean

When I type that in, I get no error, but nothing happens. And when I go to check out another branch, I just get the same error as above.

Any reason as to why git clean would do absolutely nothing?

UPDATE:

More info. git -n and git -f do exactly the same...nothing.

7

There are 7 answers

2
Venkataraman R On

If you want to clean untracked files, use the below command. But, take care before running this, as it will wipe your working area, index, HEAD.

git reset --hard

I think you are having untracked directories or ignored files(build files) of gitignore in your working area. you can remove them by the below command.

git clean -dfx

-d

Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if you really want to remove such a directory.

-f --force

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete directories with .git sub directory or file unless a second -f is given.

-x

Don’t use the standard ignore rules read from .gitignore (per directory) > and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e > options. This allows removing all untracked files, including build products. This can be used (possibly in conjunction with git reset) to create a pristine working directory to test a clean build.

For more information git clean

0
saravana va On

try to Use

git clean -fd

Try stashing of the changes,

git stash

if you want to beck all the changes use,

git stash apply
2
Spacemoose On

It seems to me you need to specify -n or -f. From the man pages:

OPTIONS

   -f, --force
       If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or
       directories unless given -f, -n or -i. Git will refuse to delete directories with .git sub directory or file unless a second
       -f is given.
1
gzimmers On

Like other comments have specified, you do need to add the -f flag.

But you may be having problems not because you aren't specifying --force, but rather because you're in a different subdirectory.

for example, if I have git repository residing in /myrepo/.git

and untracked files in subdirectory /myrepo/untracked/file.txt,

but my terminal's current working directory is /myrepo/src/.

git clean -fd will not work. You need to cd to the project root before running the command.

TL;DR: git clean -fd apparently only cleans untracked files and directories at and below your terminal's current working directory.

0
olejorgenb On

My guess this is because git clear -f do not recurse into untracked directories even though the docs reads:

Cleans the working tree by recursively removing files that are not under version control, starting from the current directory.

...

The -d docs clears up the confusion:

Normally, when no <path> is specified, git clean will not recurse into untracked directories to avoid removing too much. Specify -d to have it recurse into such directories as well.

...

2
johnny On

This is possibly not what happened in OPs case. However I had the same problem. In my case the directory that wasn't cleaned was a submodule I added, but I subsequently git reset it. I had to remove all traces of it from .gitmodules, .git/config and .git/modules/<path to submodule> for git clean to remove it even if git status recognized it as untracked.

0
caveman On

I always use git clean -df -f or git clean -dfx -f (if I wish to remove ignored files as well) as this is what the official docs for git say:

-f
--force
If the Git configuration variable clean.requireForce is not set to  
false, git clean will refuse to delete files or directories unless  
given -f or -i. Git will refuse to modify untracked nested git  
repositories (directories with a .git subdirectory) unless a second - 
f is given.