How to Remove Local Untracked Files From Current Git Working Tree?
Last Updated :
21 Jun, 2024
When working on a Git repository, it’s common to create temporary files or directories that you no longer need. These untracked files can clutter your working tree and make it harder to focus on the changes that matter. Fortunately, Git provides simple commands to clean up these untracked files. This article will guide you through the process of removing local untracked files from your current Git working tree.
What Are Untracked Files in Git?
Untracked files are those files in your working directory that are not being tracked by Git. They have not been added to the staging area using the git add
command, and therefore, are not part of any commit. These files can be newly created files, temporary files generated by your development tools, or any files that you haven't yet decided to include in version control.
Steps to Setup a Basic Project
Step 1: Create a folder named git
mkdir git
Step 2: Switch to the git folder and create a folder named files
cd git
mkdir files
Step 3: Create three files named tracked.txt, untracked.txt, and .gitignore in the git folder
touch tracked.txt untracked.txt .gitignore
Step 4: Create two files in the files folder names file1.txt and file2.txt
cd files
touch file1.txt file2.txt
Step 5: Create a .env file
touch .env
Step 6: Initialize an empty git repository using the following command
git init
Step 7: Add .env to the .gitignore file
Step 8: Add the tracked.txt and .gitignore files to the staging area using the following command
git add tracked.txt .gitignore
Let's go through each of the interactive commands one by one:
- clean: Removes the untracked files and directories and quits.
- filter by pattern: Lists all the files and directories that are going to be removed and asks the user for an input prompt in the form of a pattern to exclude files and directories from removal.
- select by numbers: Lists all the files and directories that are going to be removed and asks the user for an input prompt in the form of numbers that indicate the file number to be excluded. The number can be comma-separated or separated by white space. Example: 5,8,9 or 5-9 or 5 8 9.
- ask each: Allows the user to confirm each file that is going to be removed. Not as efficient as the above methods.
- quit: Quits the interactive mode without cleaning.
Project Structure
The project structure will be similar to the below image.

Now that the basic project structure is set up, let's understand how untracked files are removed. Untracked files are those files that are absent from the latest snapshot of the git repository. We can remove untracked files from the current git working tree using the following command
git clean
No untracked files can be removed by running the git clean command without any flag. By running the git clean command without any flags, a fatal error is generated and explicitly asks us to mention a flag
git clean without flagsList out the Files to be removed
To have a look at all the untracked files that will be removed, we can use the git clean command with the -n flag. This lists out all the files that are going to be removed using the git clean command.
git clean -n
By running the command in our repository we get:
git clean with -n flagList out the Files and Directories to be removed
To have a look at all the untracked files and untracked directories that will be removed, we can use the git clean command with the -n and -d flag. This lists out all the files and directories that are going to be removed using the git clean command.
git clean -n -d
By running the command in our repository we get:
git clean with -n and -d flagsRemove the unstaged files
To remove the unstaged files, we have to use the git clean command with the -f flag.
git clean -f
By running the command in our repository we get:
git clean with -f flagRemove the unstaged files and the unstaged directories
To remove the unstaged files and the unstaged directories, we have to use the git clean command with the -f and -d flags.
git clean -f -d
By running the command in our repository we get:
git clean with -f and -d flagsRemove the ignored files
To remove the untracked files and the files ignored by git, we have to use the git clean command with the -f flag and -x flag
git clean -f -x
By running the command in our repository we get:
git clean with -f and -x flagRemove all untracked files, directories, and ignored files
To remove all the untracked files, untracked directories, and ignored files, we have to use the git clean command with the -fdx flag
git clean -fdx
By running the command in our repository we get:

Entering the interactive mode
To enter an interactive git clean mode, we need to use the git command flag with the -i flag
git clean -i
By running the command in our repository we get:
git clean interactive mode
Similar Reads
How to Remove a File from Git without Deleting it Locally? Git is a widely used distributed version control and source code management system. It effectively tracks changes to source code, enabling effortless branching, merging, and versioning. There may be instances where we need to remove a file from the Git repository, but want to keep it in your local f
2 min read
How To Remove Version Tracking From Project Cloned From Git? A project's version history and all Git-related metadata are included when you clone it from a Git repository. In some cases, you may want to delete this version tracking in order to start over with a clean copy of the project that has no ties to the original repository. Making templates, distributi
2 min read
How to Remove Files From Git Staging Area? The Git staging area (also known as the index) is an intermediate space where Git stores changes that are about to be committed. When you add a file to the staging area using git add, it is prepared for the next commit. However, there are situations where you might need to remove a file from the sta
2 min read
How to Remove .DS_Store Files From Git Repositories? If you're a macOS user, you've probably encountered .DS_Store files. The macOS Finder creates these files to store custom attributes of a folder, such as the position of icons and the view settings. While they are harmless in themselves, they can clutter your Git repository and cause unnecessary con
3 min read
How to Remove Deleted Files from a Git Repo? Managing files in a Git repository often involves adding, modifying, and deleting files. However, sometimes files are deleted from the disk but remain tracked in the repository. To keep your repository clean and in sync with your working directory, you need to remove these files from the repository
3 min read