How do you delete untracked local files from your current working tree?
Use git clean -f -d
to make sure that directories are also removed.
Don’t actually remove anything, just show what would be done.
git clean -n
or
git clean --dry-run
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 the -f
option twice if you really want to remove such a directory.
git clean -fd
You can then check if your files are really gone with git status
.
git clean -fd
removes directory
git clean -fX
removes ignored files
git clean -fx
removes ignored and un-ignored files
can be used all above options in combination as
git clean -fdXx
check git manual for more help
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.
I am 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
To remove all untracked files, The simple way is to add all of them first and reset the repo as below
git add --all
git reset --hard HEAD
To remove the untracked files you should first use command to view the files that will be affected by cleaning
git clean -fdn
This will show you the list of files that will be deleted. Now to actually delete those files use this command:
git clean -fd