Git Basics: How to remove untracked files from the working tree?
I hope that, you are finding my posts on Git Basics/Git Tutorial helpful. In the last post on this series, we learned " How to stash th...- Article authored by Kunal Chowdhury on .
I hope that, you are finding my posts on Git Basics/Git Tutorial helpful. In the last post on this series, we learned " How to stash th...- Article authored by Kunal Chowdhury on .
I hope that, you are finding my posts on Git Basics/Git Tutorial helpful. In the last post on this series, we learned "How to stash the changes in a dirty working directory?". Today we are going to discuss how to remove untracked files.
Everyone needs to clean the working tree when some job is done or want to reset the directory. Let's learn how to do this using the command "git clean".
The command "git clean" cleans the working tree by recursively removing files that are not under the version control. It starts the cleanup from the current directory. In case you want to shelve your dirty directory before cleaning, read out the "git stash" command.
In general, only the files unknown to Git are removed, but if you specify the '-x' option, the ignored files are also removed. When you specify '-d', the untracked directories also get cleaned in addition to the untracked files. You can combine all the parameters together to have a clean working set of the files and directories.
Here is the list of command parameters, which you will find useful while working with Git repositories to clean the current working tree.
Remove untracked files from the current working tree:
$ git clean
Remove untracked directories in addition to untracked files:
$ git clean -d
Forcefully clean the current working tree:
$ git clean -f
$ git clean --force
Clean working tree in interactive mode:
$ git clean -i
$ git clean --interactive
Clean working tree without using the standard ignore rules:
$ git clean -x
Remove only files ignored by Git:
$ git clean -X
Clean working tree to default state:
$ git clean -xfd
$ git clean -x -f -d
Was this post useful? If you came to this page by searching online about “git basics” or “git tutorials”, please have a look into my other blog posts. Subscribe to the RSS feed or the email newsletter to keep yourself updated.

Solution Architect & Former Microsoft MVP
Kunal Chowdhury is an enterprise solution architect and former multi-year Microsoft MVP. He is the author of three technical books: Windows Presentation Foundation Development Cookbook, Mastering Visual Studio 2017, and the Mastering Visual Studio 2019.
He publishes technical and non-technical articles on Kunal-Chowdhury.com.