Sep 20, 2023 3 min read

How to Remove Untracked Files in Git

Remove Untracked Files in Git with our step-by-step tutorial. Files in the Git working directory are either tracked or untracked.

Remove Untracked Files in Git
Table of Contents


Before we begin talking about how to remove untracked files in Git, let’s briefly understand - What are tracked and untracked files in Git?

Files in the Git working directory are either tracked or untracked. Tracked files are those which are added and committed, and Git knows about them. They are easily unmodifiable, modifiable, or can be staged. All other files in the working directory are untracked and Git is not aware of them. So, sometimes a user's Git working directory may get cluttered up. It will be with unnecessary files either auto-generated, leftover from merges, or created by mistake. In those situations, the user can add those files in .gitignore. They can also remove them if they want to keep their repository nice and clean.

In this tutorial, you will remove untracked files in Git. We will also address some of the FAQs related to tracked and untracked files.

Step 1 - Remove Untracked Files

1) Firstly, git clean is used to remove untracked files. It is always recommended to take a backup of the repository because once deleted it will not be possible to recover them.

With Git you can perform a "dry run" which shows you what all files or directories will be deleted, and you can double-check the files before running the actual command.

git clean -d -n 

The output will be like this:


Would remove content/test/
Would remove content/blog/post/

If few files listed above are necessary then you can start tracking these files with git add <file> or you can add them to your .gitignore.

2) Once you are sure of going ahead and deleting the files, you need to type the following command:

git clean -d -f

The command will print all deleted files and directories:


Removing content/test/
Removing content/blog/post/

3) -d option will tell Git to remove the untracked directories too. If you do not want to delete the empty untracked directories, you will need to omit -d option.

4) The -f option stands for force. If not useful and the Git configuration variable clean.requireForce is set to true, Git will not delete the files.

To interactively delete untracked files, use -i option:

git clean -d -i

The output will show all files and directories to be removed. I will ask you what to do with those files:


Would remove the following items:
  content/test/   content/blog/post/
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers
    4: ask each             5: quit                 6: help

5) Proceed to select one of the choices and hit Enter. If you want to limit clean operation to given directories, continue to pass the paths to directories that are to be checked for untracked files as arguments to command. For checking the files under the src directory, you need to run the following command:

git clean -d -n src

Step 2 - Removing the Ignored Files

1) git clean command also allows removing ignored files and directories too. Therefore, to remove all ignored and untracked files. You will use the -x option:

git clean -d -n -x

2) If you want to remove only ignored files and directories then you should use the -X option:

git clean -d -n -X

The above command will delete all files and directories, listed in .gitignore and keep the untracked files.

FAQs to Manage Files in Git

Is there a way to recover accidentally deleted untracked files?

No, once you have removed untracked files using Git, they are permanently deleted. It's always a good practice to keep backups or use a version control system to avoid accidental deletion.

How can I check for untracked files in Git? 

You can use the command git status to see a list of untracked files in your Git repository. Untracked files will be displayed under the "Untracked files" section.

Why should I remove untracked files in Git?

Removing untracked files is important for keeping your project clean and organized. It helps prevent accidental commits of unwanted files and reduces clutter in your repository.

Where are the files stored before they are being committed to Git?

The Git stores the complete history of the user's files for a project. It is in a special directory known as a repository, or repo. This repo is mainly present in a hidden folder called .git.

How to stage all modified and tracked files?

The git add -u looks at already staged and deleted files and not the new files. It does not add any new files. It is a safer command as compared to others as it only affects files that are already tracked by the repo.

Can I remove untracked files without affecting my committed files? 

Yes, removing untracked files will only affect the files in your local working directory and Git's index. It does not affect your committed files or any other remote repositories.

How do I check for untracked files in Git?

You can run the command git status in your project directory. Git will display a list of untracked files under the "Untracked files" section.


We hope this detailed guide helped you to remove untracked files in Git.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.