Git clean is not cleaning untracked files

DA. picture DA. · Aug 15, 2018 · Viewed 9.7k times · Source

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.

Answer

Venkataraman R picture Venkataraman R · Aug 16, 2018

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