Git: untracked working tree files and branch checkout


Untracked working tree files are basically just new files that have never been commited to your Git repository. They might prevent you from switching to another branch. You can see errors similar to this one:

$ git checkout <branch>

error: The following untracked working tree files would be overwritten by checkout:
Please move or remove them before you can switch branches.

Workaround A:
You can manually remove untracked files before checking out another branch. If you actually need the files you might want to move them to separate "backup" folder instead of deleting.

It seems OK if the number of untracked files is low. Otherwise it may be a bit of a hassle if you have a lot of them.

Workaround B:
Add the untracked folders and files to git ignore properties, for example:

One thing to note is that git ignore has no effect on files that have already been committed (which might be in a different branch) so they can be removed by running the following command:

$ git rm --cached

Downside of this solution is that they are just ignored from now on and you might need to add them back in the future.

Workaround C:
This is probably the best if you don't care about the files - just delete them all automatically by running the following command:

$ git clean -d -fx ""

-d also remove untracked directories
-f force it to run
-x also remove files ignored by git (.gitignore)
Be careful with -x as it might remove your project IDE specific settings and everything else that is ignored by git

You can find more details about git clean here:

That's it :)