git clean: delete untracked files, with a dry run first
git clean deletes untracked files; -n previews, -f deletes, -d includes directories, -x includes ignored files.
> Warning: this page deletes files. Nothing here can be restored through Git. Run every command in a throwaway directory created with mktemp -d, and read the dry run before removing the -n.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-14-A: git clean -f leaves ignored files; -x removes them; -n removes nothing
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > a.txt; git add a.txt; git commit -qm one
printf '*.log\n' > .gitignore; git add .gitignore; git commit -qm gi >/dev/null
printf 'x\n' > junk.tmp; printf 'y\n' > keep.logPaths written as ../name sit one level above the repository folder, inside the same throwaway tree, so they do not appear in git status.
Command:
git clean -n | tee ../dry.out; echo still_there=$(ls junk.tmp 2>/dev/null | wc -l); git clean -f >/dev/null; echo after_f_junk=$(ls junk.tmp 2>/dev/null | wc -l); echo after_f_log=$(ls keep.log 2>/dev/null | wc -l); echo preview_x=$(git clean -ndx | wc -l); git clean -fdx >/dev/null; echo after_x_log=$(ls keep.log 2>/dev/null | wc -l)Recorded output:
Would remove junk.tmp
still_there=1
after_f_junk=0
after_f_log=1
preview_x=1
after_x_log=0TE-14-B: git clean -f leaves an untracked directory; -fd removes it
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > a.txt; git add a.txt; git commit -qm one
mkdir -p newdir; printf 'x\n' > newdir/fCommand:
git clean -f >/dev/null; echo dir_after_f=$([ -d newdir ] && echo yes || echo no); git clean -fd >/dev/null; echo dir_after_fd=$([ -d newdir ] && echo yes || echo no)Recorded output:
dir_after_f=yes
dir_after_fd=noTE-14-C: expected non-zero exit: git clean without -f, -n or -i refuses to run and removes nothing
Setup for this run, inside a throwaway directory (mktemp -d):
git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'a\n' > f.txt; git add f.txt; git commit -qm base
printf 'u\n' > u.txtCommand:
git clean; rc=$?; ls u.txt; exit $rcRecorded output:
u.txtStandard error from the same run:
fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to cleanExit status: 128. This status and the output match what was written before the run.
TE-14-C: git clean with no option exited with status 128 and printed fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean. The ls after it printed u.txt, so the untracked file was still there.
Limits
Data loss is permanent in Git terms; dry run first; confined to the test directory.
Source
Evidence
TE-14-A: git clean -f leaves ignored files; -x removes them; -n removes nothingTE-14-B: git clean -f leaves an untracked directory; -fd removes it
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
> Warning: this example is destructive. Run it only in a throwaway directory, not against real files.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-14-A | git clean -f leaves ignored files; -x removes them; -n removes nothing | 0 | PASS | 3b4516d4e967 | | TE-14-B | git clean -f leaves an untracked directory; -fd removes it | 0 | PASS | 73597514e016 | | TE-14-C | expected non-zero exit: git clean without -f, -n or -i refuses to run and removes nothing | 128 | PASS | 7f648fbf6431 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides