How to remove ignored files and directories?

git clean -f -d -X

To remove files and directories ignored by git use git clean -f -d -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 -d parameter tells git to clean files recursively and remove directories as well.

The -X parameter tells git to remove ignored files only, but keep the untracked ones.

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

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

See git clean to learn more about it.

Last modified on November 30, 2021.

You might also like