git clean Command: Remove Untracked Files in CI
git clean removes untracked files (and optionally directories and ignored files) from the working tree.
On reused runners, leftover build artifacts from a previous job can poison the next one. git clean -fdx is the standard way to scrub the tree back to a pristine, tracked-only state.
Common flags
-f/--force- actually delete (required; clean refuses to run without it)-d- also remove untracked directories-x- also remove ignored files (build outputs, caches)-X- remove only ignored files, keeping other untracked ones-n/--dry-run- show what would be deleted without deleting-e <pattern>- exclude paths from deletion
Example
shell
# Preview, then fully scrub the tree on a reused runner
git clean -ndx
git clean -fdxIn CI
git clean -fdx gives a pristine working tree on long-lived or warm runners, removing stale artifacts that cause flaky, hard-to-reproduce build failures. Always dry-run with -n first if a script is new, since -x also deletes ignored caches you may want to keep. Latchkey managed runners are ephemeral, so each job already starts clean.
Key takeaways
- git clean -fdx removes untracked files, directories, and ignored artifacts in one pass.
- -f is mandatory; clean refuses to delete without it.
- Dry-run with -n before -x, which also wipes ignored caches.
Related guides
git reset Command: Move HEAD in CIgit reset moves HEAD and optionally the index and working tree. Reference for --hard and --soft to roll back…
git status Command: Check Tree State in CIgit status shows working tree and index state. Reference for --porcelain, the stable machine-readable format…
git stash Command: Shelve Changes in CIgit stash saves uncommitted changes aside. Reference for push, pop, and --include-untracked to clean the tree…
git gc Command: Repack Repos in CIgit gc compresses and cleans up a repository. Reference for --prune, --aggressive, and --auto to keep cached…