Deleting untracked files in Git

git clean command is used to remove untracked files in the working tree.

To delete all untracked files in the current directory, a safe way is to do 2 steps:

# Step 1, check what files will be deleted. 
# -n or --dry-run,  show what files will be deleted without actually deleting them.
$ git clean -n -d

# Step2, do actual deleting action.
# Delete untracked files and untracked directories. 
# -d, delete untracked dirctories. 
# -f, force to delete, if 'clean.requireForce' in Git confiuration is set to false, 
# 'git clean' (without '-f' option) will clean nothing .
$ git clean -f -d

To remove untracked files with or without ignored files:

# Delete only ignored files.
$ git clean -f -X

# Delete untracked files, untracked directories and ignored files.
$ git clean -f -d -x

Listing untracked files in Git

List all the untracked files, including ignored files

git ls-files command is used to list files, add -o or --others to list only untracked files:

# List all the untracked files, including the ignored files
$ git ls-files --others

List untracked files, excluding ignored files

--exclude-standard option excludes files in .gitignore, .git/info/exclude or the user’s global exclusion file.

# List untracked files, excluding the ignored files.
$ git ls-files --others --exclude-standard