git clean

Basic Snapshotting

Remove untracked files from the working tree


The 'git clean' command deletes untracked files — files Git has never stored, such as logs, build output or experiments. Because Git has no copy of them, deleted files cannot be recovered. For that reason git clean refuses to run without -f, and git clean -n shows what would be deleted first. Files in .gitignore are kept unless you add -x.

Synopsis

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] [<pathspec>...]
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (3)

Do not delete anything, just show what would be deleted.

RecommendedAlways preview before deleting files for good

Example

git clean -n

Actually delete the files. Required unless clean.requireForce is set to false.

When neededRequired to delete; files are gone for good

Example

git clean -f

Also delete untracked folders, not only untracked files.

When neededNeeded when leftovers are whole folders

Example

git clean -fd

Intermediate (1)

Also delete files that .gitignore normally protects.

Not recommendedAlso deletes ignored files such as .env or local settings

Example

git clean -f -x

Choosing the Right Option

What each command removes

Tracked vs untracked

CommandUntracked filesIgnored filesEdits to tracked files
git clean -fDeletedKeptKept
git clean -f -xDeletedDeletedKept
git restore .KeptKeptDiscarded
git stash -uStashedKeptStashed

Examples

Preview

git clean -n

Lists the untracked files that would be deleted.


Delete untracked files

git clean -f

Deletes them for good.


Folders too

git clean -fd

Also deletes untracked folders.

Workflow Examples

1
git status -s

See the untracked files (??)

2
git clean -n

Preview the deletion

3
git clean -f

Delete them

Troubleshooting

fatal: clean.requireForce is true and -f not given

Cause: git clean refuses to delete without -f

Run git clean -n to preview, then git clean -f

An untracked folder was not deleted

Cause: Folders need -d

Use git clean -fd

I deleted a file I needed

Cause: Untracked files have no copy in Git

It cannot be restored by Git; check your editor or backups. Next time use git stash -u

Tips & Pitfalls

Best Practices

  • ✓Always run git clean -n before git clean -f.
  • ✓Use git stash -u instead when you might need the files later.
  • ✓Keep -x for build folders you can regenerate.

Common Mistakes

  • ✗Running git clean -f without -n first and losing a file you still needed.
  • ✗Adding -x by habit and deleting ignored files like .env.
  • ✗Expecting git clean to undo edits to tracked files — use git restore for that.

FAQ

A:

No. The files were never stored in Git.

A:

No. It only deletes untracked (and with -x, ignored) files.

A:

Yes: git clean -i lets you choose files one by one.