Cleaning Untracked Files Without Regret

Cleaning Untracked Files Without Regret

21 October 2026 · By Laravel Git · 2 min read

Untracked files accumulate: scratch scripts, a dump from debugging, build leftovers from a branch with different tooling, the config copy you made “just for a minute”. git clean is how you clear them - and it is one of the few Git commands whose victims are genuinely unrecoverable, because untracked files exist in no commit, no stash, no reflog.

Always dry-run first#

git clean -nd

-n lists what would go, without touching anything. -d includes directories, which is where most of the mess lives. Read the list; if it is all junk, run it for real:

git clean -fd

That is the whole safe workflow: -n, read, then -f.

The -x warning, especially on Laravel apps#

git clean -fdx

-x also removes ignored files - everything in your .gitignore. On a Laravel project that includes .env, and git clean -fdx deletes your .env with no backup and no recovery. It also deletes vendor/ and node_modules/, which at least reinstall.

Reach for -x only when you truly mean “make this clone pristine” - and even then, copy .env aside first. The everyday clean is -fd without the x.

Pick instead of sweeping#

git clean -i

Interactive mode lists candidates and lets you choose one by one, by pattern, or toggle each. Right answer when the directory mixes junk with files you are not sure about.

What not to clean#

Framework caches are not git clean territory. Stale config, route and view caches on a Laravel app belong to php artisan optimize:clear - it knows what to rebuild and touches nothing else. Save git clean for what the framework does not own: experiment leftovers, wrong-branch artifacts, scratch files.

And when you are unsure whether something is junk, that is what git stash -u is for. A stash can be popped next week; a cleaned file cannot come back at all.

Share this post

Our network