Git Reference ยท Staging and Committing
git clean command
Reference for git clean: preview with -n, delete untracked files and directories with -f and -d, include ignored files with -x, and avoid losing work.
git clean deletes files that Git does not track. It is the counterpart of git restore, which resets tracked files: together they bring a working tree back to a pristine state. Because the deleted files were never committed, there is no way to get them back, so Git refuses to run without -f (unless clean.requireForce is set to false) and offers a dry run with -n.
By default git clean removes only untracked files in the current directory tree. Directories, and files matched by .gitignore, need extra flags.
git clean -n # dry run: show what would be removed git clean -f # remove untracked files git clean -fd # also remove untracked directories git clean -fdx # also remove ignored files
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] [<pathspec>...]
| Name | Description |
|---|---|
-n, --dry-run |
Show what would be removed and remove nothing. |
-f, --force |
Actually delete. Required unless clean.requireForce=false. Give -f twice to also remove nested repositories. |
-d |
Recurse into untracked directories and remove them. |
-x |
Do not honor ignore rules: remove ignored files as well. |
-X |
Remove only files that are ignored. |
-e <pattern> |
Add an extra ignore pattern for this run (files matching it are kept). |
-i, --interactive |
Choose what to delete from a menu. |
-q, --quiet |
Do not print each removed file. |
<pathspec> |
Limit cleaning to these paths. |
Returns: lists each removed path as Removing <path> on standard output; exit status 0 on success.
Errors: without -f, -i or -n it stops with "fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean".
Usage notes
- Always run
-nfirst. Deleted untracked files bypass the trash and are not in the reflog. - Directories need
-d. An untracked directory with files in it is left alone bygit clean -f. - Ignored files survive by default. Build output,
node_modulesand editor files stay unless you add-xor-X.-fdxresets everything that is not tracked and is the usual "fresh checkout" command. - Nested repositories (directories with their own
.git) are protected; a second-fis required to delete them. - Staged new files are not untracked. After
git add, a new file is tracked andcleanleaves it alone; usegit rm --cachedfirst. - Paths are relative to the current directory and the command only cleans below it. Use
git clean -n -- :/to check the whole repository from a subdirectory. - Combine with
git restore .(orgit reset --hard) to discard tracked and untracked changes. - The
-eoption adds protection, it is not a filter for what to delete.
Related
git restore,git reset,git rm,git stash, .gitignore- Lessons: git clean in depth, Undo basics