gitgit-clean

Why git clean -f does not remove all the untracked files?


I hit this today:

% git checkout another_branch
error: The following untracked working tree files would be overwritten by checkout:
        __version__.txt
        alembic.ini
        alembic/README
        alembic/env.py
        alembic/script.py.mako
        folder1/file1
        folder2/file2
        ....
Please move or remove them before you can switch branches.
Aborting

OK, so I'll remove untracked files:

% git clean -f
Not removing alembic/
Not removing tools/maintenance/

However, it seems that not all untracked files have been removed:

% git checkout another_branch
error: The following untracked working tree files would be overwritten by checkout:
        alembic/README
        alembic/env.py
        alembic/script.py.mako
Please move or remove them before you can switch branches.
Aborting

What's weird is that at first git checkout another_branch git knew about those particular untracked files it later complained about (alembic/README, alembic/env.py, alembic/script.py.mako).

So why git did not delete them?


Solution

  • There are two kind of untracked files: untracked files and ignored files.

    When running as git clean (-f is just to override a "safety" config option), git will only remove untracked files, when running as git clean -x it will remove both untracked and ignored files.

    For the checkout issue, it is probably triggered by different .gitignore files in the two branches.