brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git clean -X removes only ignored files like build output

Git
~/tips/tip-git-clean-ignored-only

git clean -n -d -X

Deletes build output and other ignored files while leaving your untracked work alone.

Lowercase -x and uppercase -X do very different things in git clean. The uppercase one is the safer cleanup when you only want build junk gone.

git clean -n -d -X
git clean -f -d -X

-X removes only files that your .gitignore rules ignore: build folders, logs, caches. Untracked files that aren’t ignored, like a new source file you haven’t added yet, are left alone. Lowercase -x is the opposite of careful: it removes ignored and untracked files together.

The first line is a dry run thanks to -n; it prints Would remove build/ and so on. The second line does it for real. -d includes directories. Without it, git skips ignored folders and only lists loose files, which is confusing when build/ doesn’t show up.

It’s a quick way to get back to a fresh state without cloning again, a bit like a make clean that knows about every tool.

I’d always read the dry run before adding -f. Ignored files aren’t always junk: a local .env, a database file or editor settings are often ignored too, and git clean deletes them without a copy. There’s no undo. Use -e to keep a pattern, for example git clean -f -d -X -e '!.env', or move such files elsewhere first.

Further reading: git-clean documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link