git clean
git
Usage
git clean -n | git clean -fdDeletes 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
| Flag | Meaning |
|---|---|
| -n | dry run: list what would be deleted |
| -f | actually delete; without it git refuses |
| -d | also remove untracked directories |
| -x | also remove ignored files such as .env and node_modules |
| -X | remove only ignored files |
| -i | decide interactively |
Examples
git clean -ndlists the untracked files and folders that -fd would delete
git clean -fddeletes them; git never had a copy, so there is no undo
git clean -fdxalso 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