Open In App
Related Articles

Git – Clean

Improve Article
Save Article
Like Article

Pre-requisites: Git 

Git Clean will help you to remove the files and directories which are not tracked by git. By the git clean command, you can reduce the size of the repository space and can maintain the project clean by deleting the untracked files.

How To Use The Git Clean Command?

The Git Clean command is used to clean the untracked files in the repository. If we want to remove the unwanted files then we can use the clean command in Git. When developers want to remove the untracked files in the working repository then this command is very helpful to them.

The use of the Git Clean command is as follows: 

  • git clean -n: to dry run.
  • git clean -f: forcefully file deletion.
  • git clean -f -x: delete .gitignore files
  • git clean -f -d: delete the untracked directories.

Git Clean Examples

There are certain limitations associated which are as follows: 

By default, it will not remove:

  • the .gitignore files
  • new directories which are created recently
  • index files.
  • existing commit files

Let us pictorially depict the above commands and interpret them via terminal/PowerShell as follows:  

Example 1: Using “git clean”

git clean


Example 2: Using “git clean -n”

git clean -n


Example 3: Using “git clean –force”

git clean --force


Example 4: Using “git status” (To check the status of files)

git status


Example 5: Using “git clean -fdx”

gir clean -fdx


Git Clean Common Options 

There are so many options available with git clean and each and every option has its own features. Below are some options specified. 

1. Git clean dry run: Before executing the “git clean” command it is preferred to do a dry run. Through this, you will get to know what type of files are going to be cleaned because of the “git clean” command.


git clean --dry-run

2. Git clean force: The untracked files in the git can’t be cleaned by using the git clean command. In that case, we need to do force clean then git will remove all the untracked files also.


git clean --force 

3. Git clean ignored: Git clean ignored will help you to clean or remove the files which are ignored by git.

git clean --ignored

4. Git clean interactive: Git clean interactive will ask you before deleting or removing files for confirmation. This will avoid unfortunate deleting.

git clean --interactive

5. Git clean directories: By using “–force” will remove only the untracked files if you want to remove all the directories then the following command will help you to do that.

git clean --directories

Be aware of the “git clean” command it will delete or remove the files permanently. Check twice before executing the command it will huge loss for the organization if some important files are deleted.

Git Clean Up

By cleaning git you improve the performance of git and the size of the repository will be decreased. By using the following steps you can clean your git repositories.

1. Prune remote branches: Once the remote branches are deleted or no longer needed, by using the following command you can remove references to those branches from the local repository. Helps in keeping sync with the remote repository.

git remote prune <Alias name of remote repository>

2. Remove merged branches: If sub-branches are merged into the main branch there will;l less use of those branches you can delete those branches. For that, you use the following command.

git branch -d <branch_name>

3. Delete local and remote tags: After merging sub-branches into main branches there is no use of the tags also. You can delete those tags with the help of the following.

For deleting local tags:

git tag -d <tag_name> 

For deleting remote tags: 

git push --delete origin <tag_name>

4. Clean untracked files: The main purpose of the git clean command is to remove untracked files and directories from the working directory. Take a backup before executing the following command.

git clean

Git Clean fd

If you want to remove the directories which are not tracked by git forcibly then we can use the option “-fd”

git clean -fd

“- f” is force “-d” represents directories while executing the “-fd” make sure you have taken a backup of the directories. Make sure you use the dry run option by this you get to know which files are actually going to be affected.

Interactive Mode Or Git Clean Interactive

When we execute the git clean command it will clean all the files and directories which are not tracked by git with put any interaction. If you use the –interaction option when you execute the git clean option you produce a prompt and ask for confirmation before removing the files and directories. 

git clean --interactive

It is highly recommended to use the interactive option. It shows the files and directories which are going to be removed or deleted.  

Git Clean Advantages

Git clean command had lots of advantages. They are:

  1. Removing untracked files: Git Clean is mainly used to remove the files and directories which are untacked by git. By removing the untracked files you can keep your workspace clean.
  2. More flexible: We can use multiple options with the git clean command which helps in different ways. Like -d to remove the directories, –ignored to remove the ignored file by git.
  3. Improves the performance: If the untracked files and directories are more performance of the git repository will be decreased. Using the “git clean” command helps you to delete all the untracked files.
  4. Clean up space: Siza of the repository will reduce after removing untracked files. The repository is more manageable and contains only tracked files.

Git Clean Command Limitations

Git clean command will help you delete the untracked files permanently but there are some limitations with the git clean command. They are 

  1. Files will delete permanently: Untacked files and directories will be deleted permanently from the working directory. we can’t undo the files and directories are deleted. You need to take a backup before executing the git clean command.
  2. The scope is limited: Git clean command is not able to remove the files and directories which are already tracked by git. If the files are in ignored directories you need some additional options to remove them like –ignored etc.
  3. No default interaction: Git clean command will delete all the files and directories without any warning. If you want any warning prompt then you need to use some options with git clean, like “–interactive”.
  4. Can’t remove directories: By default git clean cant remove or delete directories. For deleting directories you need to use some options like “–directories”.

Frequently Asked Questions

1. What does git clean fX do?


git fx option will delete the untracked files in the repository working directories.

2. What is dirty vs clean git?


If there was uncommitted changes it is called git dirt and if  there is untracked files we use git clean.


Git clean command will remove and delete all the unracked files permanently. This will help you to reduce the size of the repository and will help you to manage the repository more organized way. Git Clean can be combined with git rest to completely undo all commits and additions in a repository.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 20 May, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials