Git: how to delete all files outside of the history

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Discover how to remove files untracked by Git after a git reset, using git clean, the right options, and the precautions to keep in mind.

Article published on 26/03/2026, last updated on 10/08/2026

When using Git, there's a fairly classic situation that always ends up happening.

a git reset.

You go back, you change state, you undo a commit or you move your branch… and then, looking at your project, you find yourself with a bunch of files lying around.

Sometimes these are:

  • generated files
  • build folders
  • temporary files
  • pieces of an old project state

And in git status, you see a long list of "new" files appearing.

Git hasn't deleted them, because they aren't part of the tracked files at that moment. But if you want to get rid of them (other than by deleting everything by hand), here's how to do it!

The right command: git clean

To remove untracked files, Git offers this command:

git clean -f

This command removes untracked files from the current folder.

But be careful:

git clean is a destructive command.

Once the files are deleted, Git won't be able to restore them.

Simulation mode

Before running a deletion, the right reflex is to use:

git clean -n

The -n stands for dry run. It's probably the most important option of the entire command.

Git shows what it would delete, without actually doing it. If you have a lot of files appearing after a git reset, always start there.

Removing folders

By default, git clean does not delete untracked folders.

Yet after a reset, it's often entire folders that are left lying around:

  • dist/
  • build/
  • .cache/
  • old generated folders

To include folders, use:

git clean -fd
  • f to force
  • d to include directories

In many cases, after a git reset, this is the command you're really looking for.

Including files ignored by .gitignore

There's another common case.

You do a reset, then you want to start over with a truly clean project.

But some files are ignored by Git via .gitignore:

  • build folders
  • environment files
  • caches
  • generated dependencies

By default, git clean doesn't delete them.

If you also want to delete these ignored files, you need to use:

git clean -fdx

This command removes:

  • untracked files
  • untracked folders
  • files ignored by .gitignore

It's very useful for putting a project back into a truly clean state.

But it must be used with great caution.

Removing ONLY the ignored files

To delete only the files listed in .gitignore, use the -X parameter (not to be confused with -x):

git clean -fdX

Deletes only the ignored files

Conclusion

Above all, remember this:

  • git clean -n lets you check beforehand
  • git clean -fd removes files + folders
  • git clean -fdx does a complete cleanup

Finished reading this article?
Our newsletter

No spam. Only free content, news, and ever more resources to level up your skills!

Join +1500 developers

Comments (0)

to leave a comment

No comments yet

Frequently asked questions covered in this article

git reset untracked files delete files after git reset git clean -fd git clean -fdx difference git remove untracked files git untracked files after reset clean git project after reset