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.

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

About Me

Kunal Chowdhury is a Windows Platform Development MVP of Microsoft, a Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.