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

Leave a Reply