Introduction
Project development in Git can often leave users with many old, unnecessary, or untracked files. Untracked files in Git are files in the repository which have not yet been added to the repo’s tracking index via the git add
command.
Although such files do not cause issues, deleting them increases efficiency and improves organization.
In this tutorial, you will learn how to remove untracked files and folders in Git.
Prerequisites
- Git installed (Install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
Removing Untracked Git Files and Folders
When working with untracked files, it is important to know that Git is aware of those files but it doesn’t track the contents nor actively monitors their state. Thus, Git cannot take any action on untracked files.
While untracked files can be removed using the rm command, the process is time-consuming and potentially dangerous if you delete the wrong file. Using git reset
also has drawbacks as it can unintentionally roll back changes to tracked files in the repository, while the untracked files remain in the repository.
The safest, fastest, and easiest way to remove untracked files and folders is to use git clean
. The command is easy to use and has no drawbacks since it does not delete any file unless specifically told to do so. However, when you delete a file using git clean
, it cannot be recovered, so make sure you want to remove the file before running the command.
Note: An alternative to removing files with git clean
is to perform a git stash. The git stash
command temporarily shelves changes made to the repository, allowing developers to later pop or apply those changes. Git also allows users to stash untracked files.
Perform Dry Run
Performing a dry run reduces the git clean
risks to a minimum as it shows a list of files the command will delete without actually deleting them. A dry run helps users avoid accidental file deletions.
Perform a dry run by running the following command:
git clean -n
The command lists the files in the repository which it would remove.
To list the folders as well, run:
git clean -nd
The output states which files and folders the command would remove.
Remove Untracked Files
Specify the -f
(force) flag with the git clean
command to remove untracked files from the repository. The flag forces the command to execute, and it is mandatory when you want to remove untracked files. Run:
git clean -f
To remove untracked files within a specific subfolder, use the following syntax:
git clean -f [folder_path]
Remove Untracked Folders
The git clean
command also allows you to delete untracked folders by specifying the -d
flag. Run the following command to do so:
git clean -fd
The command deletes the untracked files and folders in the repository.
Remove Untracked and Ignored Files
Git ignores some files, such as Git configuration files or build object files, when they are in the ignore list. Such files are added as an exemption in the .gitignore file. Git then disregards these files and does not display them in the output of git status
. Many Git commands also completely ignore them.
However, the git clean
command allows you to remove ignored files and folders from the ignore list. Use the following commands:
- Remove only ignored files with:
git clean -fX
- Remove ignored and non-ignored files with:
git clean -fx
For example:
The command removes only the ignored file from the repository.
Exclude File from Removal
Add the -e
flag with git clean
to specify an exclude pattern or filename. The command doesn’t delete the specified files or files following the provided pattern.
For example, the command below excludes the testfile.txt from deletion:
git clean -f -e testfile.txt
The command deletes all the untracked files but excludes testfile.txt.
Git Clean Interactive Mode
Specify the -i
flag to run git clean
in interactive mode. The interactive mode provides all the above options and ensures precision when selecting which files to delete.
Enter the interactive mode by running:
git clean -i
The command opens the interactive mode command loop and shows which files and folders it would remove.
When the prompt ends with a single >
, you can pick only one of the available choices. When it ends with >>
, multiple input items are allowed. You can enter the option number or the highlighted letter, such as c
for option 1
.
The command loop prompts for one of the available actions:
1: clean
. Starts cleaning the listed files and folders and then quits.2: filter by pattern
. Shows which files and folders will be deleted and prompts you for the file patterns to ignore during deletion. Enter a space-separated list of files and folders you don’t want to delete to exclude them. Press Enter to execute and return to the main menu.3: select by numbers
. Shows a list of files and folders and prompts you to select which items to delete. The prompt ends with>>
, which means more than one selection is allowed, separated by whitespace or a comma. Ranges and (*
) are also allowed. When you filter the items, press Enter to execute and exit to the main menu.4: ask each
. The option starts removing the files, but asks for confirmation to delete each file or folder one by one. This option is slower, especially if there are many files in the repository.5: quit
. Quits the interactive mode without removing any files.6: help
. Shows how to use thegit clean
interactive mode.
For example, the following screenshot shows the usage of option 3
, selecting which items to delete:
The prompt allows you to specify all the files you want to delete. Confirm the selection with Enter to remove the selected files.
Conclusion
This tutorial showed you how to remove untracked files and folders in Git using the git clean
command. This method is a safe and efficient way to remove untracked files, without worrying about unintentionally affecting other files in the repository.
Learn more about Git in our Git tutorial for beginners, see which protocol to use for Git – SSH or HTTPS, or download a Git commands cheat sheet to always have the most important commands at your disposal.