```.gitignore 默认忽略所有文件```
.gitignore Everything by Default

原始链接: https://packagemain.tech/p/gitignore-everything-by-default

作者建议摒弃在巨大的 `.gitignore` 文件中列出所有忽略项的传统做法,转而采用相反的工作流:**默认忽略所有内容,仅显式添加需要追踪的文件。** 这种“白名单”方法可以防止意外提交 `.DS_Store`、`node_modules` 或敏感环境变量等垃圾文件。通过使用严格的 `.gitignore` 配置,开发者能够确保只有必要的文件进入存储库,从而避免复杂项目中常见的臃肿问题。 虽然这种方法并不适用于所有工作流,但它为管理冗长、多行的忽略文件提供了一种更简洁的替代方案。针对此配置的排查,作者建议使用 `git check-ignore` 来验证哪些路径已被排除。文章最后推荐将 *lazygit* 作为管理此类 Git 工作流的必备工具。

这篇 Hacker News 讨论探讨了 Git 中“默认忽略所有内容”这一颇具争议的做法——即用户在 `.gitignore` 文件中添加 `*`,然后仅显式取消忽略所需的文件。 支持者认为,这种方法可以防止仓库混乱,并避免意外提交临时文件、缓存目录或不需要的“垃圾”文件(例如与 AI 相关的配置文件 `CLAUDE.md`)。一些用户建议,全局忽略所有点文件是一种有效的折中方案,既能保持项目整洁,又无需持续手动维护。 然而,批评者警告称,这种方法存在风险且容易出错。他们认为这过于依赖手动规范;用户可能会忘记取消忽略关键文件,从而导致提交遗漏或构建失败。许多参与者坚持认为,使用标准的、针对特定项目的 `.gitignore` 模板更为可靠。其他人则建议,只需在提交前使用 `git add -p`(补丁模式)或执行 `git status` 检查,就足以防止失误,而无需付出“忽略一切”工作流带来的额外开销。归根结底,这场辩论凸显了严格且自动化的项目清理所带来的便利,与复杂开发环境中人为遗忘风险之间的矛盾。
相关文章

原文

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.

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:

This .gitignore file does exactly that:

But not these files...

  • !.gitignore - the gitignore file itself

  • !*.go - Go source files

  • !go.mod - Go module file

  • !go.sum - Go dependencies file

  • 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.

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.

Just look at this 207-lines .gitignore from typescript-go.

p.s. if you want to check if the path is ignored by git, you can run this command:

This can save a lot of head-scratching when a file you expected to track doesn’t appear in Git.

p.s. have you heard of lazygit? The best Git TUI out there, check it out.

Discuss this post on Hacker News.

联系我们 contact @ memedata.com