# git clean Command: Remove Untracked Files in CI

> git clean deletes untracked files. Reference for -f, -d, and -x (git clean -fdx) to fully reset a working tree between builds on reused runners.

Source: https://latchkey.dev/learn/command-reference/git-clean-command-reference  
Updated: 2026-06-26

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

> `git rev-parse --abbrev-ref HEAD` returns the literal string `HEAD` on a detached checkout rather than a branch name. On GitHub Actions read `github.ref_name` instead; the git command cannot know what it was checked out for.

## FAQ

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

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
