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
Keep reading
Annotating Commits with git notes: Metadata Without Rewriting History
Some things belong on a commit but not in it: review links, benchmark results, the ticket nobody remembered to reference. git notes adds them after the fact, no rebase required.
Backing Up Your Git Server So It Actually Restores
Every clone is a partial backup - but hooks, config and server state live outside the repo. Mirror, bundle, copy off-site, and rehearse the restore.
Cherry-Picking: One Commit Without the Branch
Cherry-pick copies a single commit onto your branch and leaves the rest behind. Right for hotfixes across release branches, wrong as a sync strategy.