Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

How do you delete untracked local files from your current branch?

share|improve this question
18  
This interactive git cheat sheet ndpsoftware.com/git-cheatsheet.html shows the git workspace (google gives you better results with "workspace" than "working copy"). – qneill Feb 1 '13 at 15:43
14  
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+ – VonC Jul 23 '13 at 6:00
4  
Note that you're not removing files from git branch, as branch is a reference to a commit and therefore doesn't contain untracked files. Those are only present in the working directory and have nothing to do with branches. That's probably just terminology clarification. – Pavel Šimerda Sep 14 '14 at 22:28
1  
@unegma shouldn't your .vagrant folder be in your .gitignore anyway? – Dan Hanly Jan 16 at 9:16
1  
Interesting, I didn't realise clean considered files marked in gitignore! – timhc22 Jan 16 at 11:18

12 Answers 12

up vote 3988 down vote accepted

Show what will be deleted with the -n option:

git clean -f -n

Then - beware: this will delete files - run:

git clean -f

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

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

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

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.

See the git-clean docs for more information.

share|improve this answer
88  
git clean -f works only in the directory where it's called (and subdirectories). If you want to clean the whole working copy, you should call it in its root directory. – Eduardo Bezerra Mar 8 '13 at 10:51
90  
Got it, git clean -xfd – Michelle May 14 '13 at 12:41
6  
It is also removing all files inside .gitignore. I need to delete only files/folders which are new and not in .gitignore – Kostanos Aug 5 '13 at 23:02
8  
@Kostanos If you don't want to remove files that are in .gitignore, then do not provide the -x flag. – Lo-Tan Aug 23 '13 at 18:44
14  
git clean -f :/ works as if you had run it in the root repo dir. See also later answers also accounting for submodules with git clean -ffxd :/ – here Oct 16 '14 at 20:38

git clean -f -d to be sure that also directories are gone! you can check with git status if they are really gone.

share|improve this answer
25  
As previously stated, good to dry-run it with git clean -n -d – staticelf Dec 9 '13 at 10:54

I am so surprised nobody mentioned this before:

git clean -i

That stands for interactive and you will get a quick overview of what is going to be deleted offering you the possibility to include/exclude the affected files. Overall, still faster than running the mandatory --dry-run before the real cleaning.

You will have to toss in a -d if you also want to take care of empty folders. At the end, it makes for a nice alias:

git iclean

That being said, the extra hand holding of interactive commands can be tiring for experienced users. These days I just use the already mentioned git clean -fd

share|improve this answer
1  
Definitely the fastest method while still providing notification of what will be removed – DrewT May 6 '14 at 15:11
11  
got: error: unknown switch `i' – pal4life Jun 18 '14 at 21:58
8  
@pal4life It was added in 1.8.4, you might be running an older version of git? github.com/git/git/blob/master/Documentation/RelNotes/1.8.4.txt – mabac Sep 30 '14 at 12:37
1  
@SystematicFrank, Don't use alias, they don't really scale beyond 1 person.... – Pacerier Oct 20 at 11:01

If untracked directory is a git repository of its own (e.g. submodule), you need to use -f twice:

git clean -d -f -f

share|improve this answer
    
this is a command which should be remembered. – Vigh Iosif Apr 15 at 13:39
5  
Or shorthand: git clean -dff – Dmitry Ginzburg Jun 26 at 16:47
    
BTW, this is written in documentation : Git will refuse to delete directories with .git sub directory or file unless a second -f is given. But thanks anyway! – Maxim Suslov Jul 24 at 9:03

I like git stash save -u because you can undo them all with git stash pop.

EDIT: Also I found a way to show untracked file in a stash (e.g. git show stash@{0}^3) http://stackoverflow.com/a/12681856/338986

share|improve this answer

git-clean is what you are looking for. It is used to remove untracked files from the working tree.

share|improve this answer
    
same comment as previous : linux.die.net/man/1/git-clean – dlewin Nov 3 '11 at 14:04

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.

share|improve this answer

This is what I always use:

git clean -fdx

For a very large project you might want to run it a couple of times.

share|improve this answer

git clean -fd removes directory

git clean -fX removes ignored files

git clean -fx removes ignored and un-ignored files

check git manual for more help

share|improve this answer

A better way is to use: git clean

git clean -d -x -f

This removes untracked files, including directories (-d) and files ignored by git (-x).

Also, replace the -f argument with -n to perform a dry-run or -i for interactive mode and it will tell you what will be removed.

share|improve this answer

Someone should really mention:

git clean [<options>]

Am I right?

share|improve this answer

protected by Elenasys Jan 13 '14 at 23:49

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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