.gitignore Everything by Default
.gitignore Everything by Default
.gitignore everything by default. An opposite view on .gitignore files. 默认忽略一切(.gitignore everything by default)。关于 .gitignore 文件的一种反向思维。
I think we’ve all been there. You’re working on a project, making commits, and then suddenly realize you’ve been committing .DS_Store files, node_modules, IDE configuration files, or other junk (CLAUDE.md for example) that shouldn’t be in your repository. Or even worse - environment variables. Then comes the embarrassing cleanup: adding these files to .gitignore, removing them from the repository history, and hoping that no one has noticed.
我想我们都经历过这种情况:在进行项目开发并提交代码时,突然发现自己把 .DS_Store 文件、node_modules、IDE 配置文件或其他不该出现在仓库里的垃圾文件(例如 CLAUDE.md)都提交了上去。更糟糕的是,甚至还提交了环境变量。随之而来的就是尴尬的清理工作:将这些文件添加到 .gitignore,从仓库历史中删除它们,并祈祷没人注意到。
What if we flipped this approach entirely? Instead of allowing everything by default and selectively ignoring files, what if we ignored everything by default and only allowed specific files? 如果我们彻底颠覆这种做法会怎样?与其默认允许所有文件并有选择地忽略某些文件,不如默认忽略所有文件,只允许特定的文件通过?
Here’s what this could look like in practice for a simple Go project: 以下是一个简单的 Go 项目中这种做法的实际应用示例:
*
!.gitignore
!*.go
!README.md
!go.mod
!go.sum
This .gitignore file does exactly that:
这个 .gitignore 文件实现的功能如下:
-
- Ignore everything
-
- 忽略所有内容
But not these files… 但以下文件除外……
!.gitignore - the gitignore file itself
!.gitignore - .gitignore 文件本身
!.go - Go source files !.go - Go 源代码文件
!go.mod - Go module file !go.mod - Go 模块文件
!go.sum - Go dependencies file !go.sum - Go 依赖文件
anything else you wish to include 以及任何你希望包含的其他文件
With this setup, only the files you explicitly allow will be tracked by Git. No more accidental commits of unintended local files. 通过这种设置,只有你明确允许的文件才会被 Git 追踪。再也不会意外提交不必要的本地文件了。
The technique isn’t necessarily the right choice for every repository or developer, but is an alternative to explore. 这种技术未必适合每一个仓库或开发者,但它是一个值得探索的替代方案。
Nowadays, I see that projects have so much crap locally, like various agentic docs or subfolders, so it may seem easier to just ignore everything at first. 如今,我发现项目本地充斥着大量垃圾文件,比如各种 AI 代理文档或子文件夹,因此起初就忽略所有内容似乎更容易一些。
Just look at this 207-lines .gitignore from typescript-go.
看看这个来自 typescript-go 项目长达 207 行的 .gitignore 文件就知道了。
p.s. if you want to check if the path is ignored by git, you can run this command: 附注:如果你想检查某个路径是否被 git 忽略,可以运行以下命令:
git check-ignore -v internal/server/server.go
This can save a lot of head-scratching when a file you expected to track doesn’t appear in Git. 当你期望追踪的文件没有出现在 Git 中时,这可以省去很多抓耳挠腮的困惑。
p.s. have you heard of lazygit? The best Git TUI out there, check it out. 附注:你听说过 lazygit 吗?它是目前最好的 Git 终端用户界面(TUI),不妨试一试。