21

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.

6
  • 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 Commented Aug 15, 2018 at 21:06
  • 1
    @WilliamPursell Thanks. I updated my question. Alas, -f and -n also result in nothing happening.
    – DA.
    Commented Aug 15, 2018 at 21:12
  • 1
    what does git status show?
    – Spacemoose
    Commented Aug 15, 2018 at 21:18
  • 3
    From which directory are you running git clean? It will only clean the current working directory and below. Commented Aug 15, 2018 at 21:22
  • 4
    Are those files being ignored? Did you try adding -x? Commented Aug 15, 2018 at 21:33

7 Answers 7

32

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

2
  • 1
    I was missing that X... it wasn't cleaning =(
    – Daniel N.
    Commented Apr 15, 2022 at 7:51
  • all of this is not helps( Commented Jul 28, 2023 at 8:45
14

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.

1
  • 1
    thanks! this was my issue (when working in a yarn workspace mono repo)
    – Norfeldt
    Commented Nov 16, 2021 at 7:21
4

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.  

3

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.

2
  • 1
    I was not able to run git clean (likely due to git module issues); I ended up doing: "rm -rf <folders containing the files>; git reset --hard"
    – Qiang Li
    Commented Sep 3, 2020 at 1:44
  • In my case I moved some submodules using git mv. I couldn't find anything in .gitmodules or .git that looked like it should actually be removed, so I just removed those directories manually. Very annoying.
    – Max
    Commented Dec 14, 2020 at 15:04
2

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
1

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.
2
  • 1
    I updated my question. Alas, -f and -n also result in nothing happening.
    – DA.
    Commented Aug 15, 2018 at 21:11
  • Thanks for sharing this. In my case, I had to add two -f arguments to get all of my ignored files and directories deleted.
    – firebush
    Commented Mar 22, 2022 at 16:09
1

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.

...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.