../

How to remove untracked git files

July 19, 2022


Reverting changes

If you simply want to revert your changes to the most recent commit:

git reset --hard

To remove files that are untracked, we'll use git clean.


Dry run

git clean has a dry run option (-n) where we can preview any changes that would be made.

git clean -n

This shows any files in the current directory that would be deleted. git clean avoids recursing into further directories by default. To recurse into other folders, use the -d option.

git clean -nd

Let's go!

To actually clean the files, use -f.

git clean -f

Again, this removes files in your current directory. To remove files recursively in subdirectories, use -d.

git clean -fd

To clean files that are ignored (i.e., they're in .gitignore), use -x

git clean -fx

For an interactive experience, use -i. This can also be paired with -d.

git clean -id

Summary

  • use -n to preview any changes that may be made
  • use -d to recurse into subdirectories
  • to actually delete, use -i for an interactive experience or use -f to force

Copyright © 2023 AJ Futo