How to remove untracked and ignored files?

git clean -f -x

To remove untracked and ignored files use git clean -f -x.

Since git clean removes files it's recommended to start with a dry-run using parameter -n. A dry-run will show what is going to be removed without actually removing them.

The -f parameter tells git to remove the files when git clean is used. The usage of parameter -f is required unless clean.requireForce is configured to false.

The -x parameter tells git to remove both the untracked and the ignored files.

Git clean -f -x won't remove ignored directories. Add parameter -d to remove directories as well.

Layer 1
Terminal Example
cat .gitignore

git clean -n -x
Would remove ignoredFile.txt
Would remove untrackedFile.txt
git clean -f -x
Removing ignoredFile.txt
Removing untrackedFile.txt

See git clean to learn more about it.

Last modified on November 30, 2021.

You might also like