Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Git clean: remove untracked files

Temporary files, logs, builds cluttering your project? git clean tidies up your working directory by removing everything that isn't tracked by Git.

Warning

What is git clean?

git clean removes untracked files from your working directory. These are files Git doesn't know about: they were never added with git add.

It's useful for starting fresh: removing temporary files, debug logs, build artifacts.

Warning: git clean is irreversible. Deleted files cannot be recovered. Always use -n (dry run) before -f (force).

Git clean options

git clean -n

Dry run: shows what would be removed

git clean -f

Force: removes untracked files

git clean -fd

Removes untracked files AND directories

git clean -fx

Also removes ignored files (.gitignore)

git clean -i

Interactive mode: choose file by file

Git clean in practice

Always check with -n before deleting with -f.

Dry run then clean

With directories and ignored files

When to use git clean?

Clean up after a build

Remove build artifacts (dist/, build/, .cache/) to start fresh before a new build.

Remove test files

Temporary files created during debugging (logs, exports, screenshots) that are not meant to be committed.

Start from scratch

Combined with git checkout ., you can return to a state identical to the last commit, with no extra files.

Safety rules with git clean

!

Always -n before -f

Run git clean -n to see what will be deleted. Then git clean -f only if the list is correct.

!

Be careful with -x

The -x flag also removes files ignored by .gitignore. This includes .env, node_modules, etc. Use it with caution.

+

Prefer interactive mode

git clean -i lets you choose file by file what to delete. Slower but safer.

+

Check your .gitignore

Make sure your .gitignore is up to date. Files that are neither ignored nor tracked will be removed by git clean.

Part of the Undo Changes in Git guide

A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions about git clean

Learn to clean your repo safely

GitQuest offers scenarios where you need to use git clean in a safe environment.

Start practicing now