Skip to content
LatchkeyLatchkey home

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 -fdx

In 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.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

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.

Frequently asked questions

git clean Command: Remove Untracked Files in CI?
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.
In 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.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card