Home·Terminal commands

git clean

git

Usage

git clean -n | git clean -fd

Deletes untracked files, which git never had a copy of, so nothing comes back; -fdx also wipes ignored files such as a local .env, so run -n first.

Common flags

FlagMeaning
-ndry run: list what would be deleted
-factually delete; without it git refuses
-dalso remove untracked directories
-xalso remove ignored files such as .env and node_modules
-Xremove only ignored files
-idecide interactively

Examples

git clean -nd

lists the untracked files and folders that -fd would delete

git clean -fd

deletes them; git never had a copy, so there is no undo

git clean -fdx

also wipes ignored files, including local .env and build output

The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does git clean do?

Deletes untracked files, which git never had a copy of, so nothing comes back; -fdx also wipes ignored files such as a local .env, so run -n first.

Q. How do I type it?

git clean -n | git clean -fd — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man git. This command sits under git.

Related commands

man page: man git-clean