Removing untracked files from your Git working copy
22
How do you delete untracked files from your Git working copy?
improve this question | comment
Gino Medhurst Created at: 2013-11-13 17:07:13 UTC By Gino Medhurst
Considering the traffic to this question, it seems worth clarifying what a "working copy" is.  Google wasn't cutting it, so from Peepcode's Git Internals... "working directory is the checkout of the current branch you are working on... Git will make your working directory look like that branch, removing any checked in content that is currently in your working directory that is not in the new tree. [This is why all changes must be committed before switching branches -AG]. Your working directory is just a copy of a tree so you can edit it and commit changes" - Mr. Margot Nitzsche
This interactive git cheat sheet ndpsoftware.com/git-cheatsheet.html shows the git workspace (google gives you better results with "workspace" than "working copy"). - Ms. Bertha Hermiston
Note: if you only want to remove some untracked files, but not all of them, git clean has now an interactive mode! See my answer to this other question: git 1.8.4+ - Haskell Maggio
6 Answers
0
If needed to remove untracked files from particular subdirectory,

git clean -f {dir_path}


And combined way to delete untracked dir/files and ignored files. 

git clean -fxd {dir_path}


after this you will have modified files only in git status.
0
git-clean - Remove untracked files from the working tree
0
git clean -f


But beware... there's no going back. Use -n or --dry-run to preview the damage you'll do.

If you want to also remove directories, run git clean -f -d

If you just want to remove ignored files, run git clean -f -X 

If you want to remove ignored as well as non-ignored files, run git clean -f -x

Note the case difference on the X for the two latter commands.

If clean.requireForce is set to "true" (the default) in your configuration, then unless you specify -f nothing will actually happen, with a recent enough version of git.

Note that as of git-1.6.0, the dashed style of writing git commands (ie, git-clean instead of git clean) is obsoleted.

See the git-clean docs for more information.
0
If untracked directory is a git repository of its own (e.g. submodule), you need to use -f twice:

git clean -d -f -f
0
git clean -f -d to be sure that also directories are gone!
you can check with git status if they are really gone.
0
git-clean is what you are looking for. It is used to remove untracked files from the working tree.
Your Answer