Alex Pliutau over at Package Main has some sage advice about Git usage that hadn’t occurred to me. The advice is simple: ignore everything by default. The idea is to avoid filling up your repository with junk. Things like generated files, OS generated files having nothing to do with your project, IDE configuration files, AI slop, and even—most critically—secret credentials that are somehow always seeping into git repositories.
All that happens because you didn’t think to add them to the .gitignore file. And it’s an ongoing problem: new files that shouldn’t get tracked are being created all the time. Pliutau’s idea is simple. Flip things around by ignoring everything by default and then add the files you do want to track explicitly.
That turns out to be easy. You simply add * as the first line of your .gitignore file telling it to ignore everything. Then you add the exceptions to that policy on subsequent lines. See Pliutau’s post for any example. You don’t, of course, have to add, say, each C file by hand. A simple !*.c gets them all.
I really like this idea. Like most great ideas, it’s simple yet powerful. I wish it had occurred to me early on. My .emacs.d repository, in particular, is a total mess with all sorts of junk that shouldn’t be in there. It’s been on my list of things to clean up for years. Maybe Pliutau’s hint will inspire me to finally do so.