As Git source code versioning software uses tracked and untracked terms to track files. We untracked files are generally added newly to the repository but there may be some bulk files those are unnecessary to version. In this tutorial, we will learn different ways to remove untracked files with Git.
List Files To Be Deleted
Before deleting untracked files and folders we will list untracked files and folders. We will use clean
command with the -n
option like below.
$ git clean -n

Delete or Remove Untracked Files and Folders Forcibly
The most basic way to remove untracked files and folders is using clean
git command with the -f
option like below. -f
means remove and clean forcibly without asking anything.
$ git clean -f

Remove Untracked Directories Only
If we just need to remove or delete untracked directories we shouşd use -d
option with the remove command. -d
simply means removing only untracked directories.
$ git clean -f -d
Remove Untracked Files Only
If we just want to remove untracked files only we need to use -X
option like below.
$ git clean -f -X
Simulate Remove Of Untracked Files and Directories with Dry Run
Removing untracked files and directories may be a critical job where we can lose our latest work. So we can simulate or dry run removing untacked files with the --dry-run
option like below.
$ git clean -d --dry-run

Normally, when no path is specified, git clean will not recurse into untracked directories to avoid removing too much. Specify -d to have it recurse into such directories as well. If any paths are specified, -d is irrelevant; all untracked files matching the specified paths (with exceptions for nested git directories mentioned under