How to remove untracked and ignored files and directories?

git clean -f -x -d

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

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.

The -d parameter tells git to clean files recursively and remove directories as well.

Layer 1
Terminal Example
cat .gitignore
ignoredFile.txt
ignoredDir/

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

See git clean to learn more about it.

Last modified on November 30, 2021.

You might also like