Git Remove Untracked Files

If you want to remove untracked files from the working tree in Git, using the git clean command is the best way. The git clean command cleans the working tree by recursively removing the files which are not under version control.

Difference between Tracked vs. Untracked Files

Before getting into the depth, let us first understand the difference between tracked and untracked files.

Tracked Files: Tracked files are the ones that are added and committed to the version control in the previous commit or snapshot, and Git would be tracking these files for changes.

Untracked Files: Untracked files are precisely the opposite. These files are not added or committed to the version control. These files are newly added and ready to be staged and committed to the repository.

The git status command will show all the tracked files that are modified and also the untracked files.

git-clean Documentation

Now that we have understood the difference between tracked and untracked files. Let us look at cleaning up untracked files in Git.

The git-clean command is handy in cleaning up untracked files in Git.

Syntax

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…​

The git clean command comes with many options, and here we will look at some of the commonly used options, which is helpful.

Options

-d

Usually, when no <path> is specified, git clean will not recurse into untracked directories to avoid removing too much. If the path is not specified, the -d flag will recursively clean the files in all the directories.

 -f

--force

If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f or -i. Git will refuse to modify untracked nested git repositories (directories with a .git subdirectory) unless a second -f is given.

-n

–dry-run

The -n option will not remove anything, and it’s a dry run which just shows what would be done.

-X

Remove only files ignored by Git. It will be helpful to rebuild everything from scratch but keep manually created files.

How to Remove Untracked Files in Git?

Starting with a “Dry Run”

Before performing git clean to delete untracked files, it’s always recommended to do the dry run since these are untracked files once you delete them, you will not be able to recover them as it’s not in the source control.

# Print out the list of files and directories which will be removed (dry run)
$ git clean -n -d

The above command will list all the files and directories which are untracked.

Output

Would remove file2.txt
Would remove js/
Would remove test.txt

Deleting Files with the -f Option

If you want to delete the files in your working copy, you need to use the force option as shown below.

$ git clean -f

If you want to only delete untracked files in a specific subdirectory of your project, you need to specify the path of the subdirectory.

$ git clean -f folder/subfolder

The git clean command will not delete the directories by default. If you want to delete directories, then you need to pass the -d flag.

$ git clean -fd

In some cases, if you want to delete any ignored files, you need to pass the -x flag, which includes the ignored items for deletion.

$ git clean -fx
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like