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".

 

How to remove untracked files from working tree using Git Clean command (www.kunal-chowdhury.com)

 

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.

 

 

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.