`.gitignore` 并不是 Git 中忽略文件的唯一方式
.gitignore Isn't the Only Way to Ignore Files in Git

原始链接: https://nelson.cloud/.gitignore-isnt-the-only-way-to-ignore-files-in-git/

Git 提供了三个层级的忽略文件设置,支持仓库级和全局配置: 1. **.gitignore:** 位于仓库根目录,会被提交到版本控制中,适用于该仓库的所有用户。 2. **.git/info/exclude:** 仅对特定仓库有效,不会被 Git 追踪。适合用于忽略个人工作流中特有的文件(例如个人笔记),且不会影响他人。 3. **~/.config/git/ignore:** 全局配置文件,适用于本机的所有仓库。用于忽略系统生成的特定文件(如 `.DS_Store`)。你可以使用 `git config --global core.excludesFile` 来自定义此文件的位置。 若要确定某个文件是由哪个配置文件忽略的,请使用以下命令: `git check-ignore -v <文件名>` 该命令会输出导致忽略规则的具体文件路径及行号。如果命令没有输出任何内容,则说明该文件未被上述任何来源所忽略。

```Hacker News 新闻 | 过往 | 评论 | 提问 | 展示 | 招聘 | 投稿 登录 .gitignore 并非 Git 中忽略文件的唯一方式 (nelson.cloud) 40 分 | 作者 FergusArgyll | 3 小时前 | 隐藏 | 过往 | 收藏 | 2 条评论 | 帮助 Hendrikto 5 分钟前 [–] 这只不过是对这篇文档的低质量复述:https://git-scm.com/docs/gitignore 回复 jagged-chisel 0 分钟前 | 父评论 [–] 嘿,别这样——他们补充了关于 'check-ignore' 的内容,这算是不错的补充建议。 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 加入 YC | 联系 搜索: ```
相关文章

原文

I’ve been using Git for so long and I just realized you can ignore files at three different levels and not just with .gitignore. The three files you can use to ignore files are:

  • .gitignore
  • .git/info/exclude
  • ~/.config/git/ignore

.gitignore is the usual file where you write files you want to ignore. It’s checked into Git along with the rest of the code. Whatever files you add to it will not get taken into account when running git commands.

The exclude file lives in the .git directory of every Git repository but changes to it are not checked into Git. It usually has a few comment lines on a fresh Git repository. This file is useful for ignoring things on a per-repo basis. For example, you may have a personal notes.txt file in a repository that you don’t want to check into git but you also don’t want to add to .gitignore because it’s unique to your workflow. In that case you would add notes.txt to .git/info/exclude.

The ignore file lives in your machine’s home directory in ~/.config/git/ignore. Whatever filenames are added to this file are ignored globally at a machine-level. This file is not checked into Git and isn’t associated with any particular repository. It’s a great place to add files that you want to ignore in every git repository on your computer. For example, if you’re on macOS, adding .DS_Store here would be ideal.

You can customize the global ignore file to be a different file. For example, if you want your global git ignore file to be .gitignore_global you would run the command:

Shell
git config --global core.excludesFile ~/.gitignore_global

And if you ever want to return to the default setting, run:

Shell
git config --global --unset core.excludesFile

When adding filenames to any of these, you can use this command to check how a filename is being ignored. For example, if you want to check how .DS_Store is being ignored, run git check-ignore -v .DS_Store in any Git repository.

Here is the output when the repository’s .gitignore is ignoring .DS_Store:

Console
$ git check-ignore -v .DS_Store
.gitignore:1:.DS_Store	.DS_Store

Here is the output when the repository’s .git/info/exclude is ignoring .DS_Store:

Console
$ git check-ignore -v .DS_Store
.git/info/exclude:7:.DS_Store	.DS_Store

Here is the output when the global ~/.config/git/ignore file is ignoring .DS_Store:

Console
$ git check-ignore -v .DS_Store
/Users/nelson/.config/git/ignore:2:.DS_Store	.DS_Store

And here is the output when a custom global ignore file .gitignore_global is ignoring .DS_Store:

Console
$ git check-ignore -v .DS_Store
/Users/nelson/.gitignore_global:1:.DS_Store	.DS_Store

If there is nothing ignoring a file, the git check-ignore -v command produces no output.

联系我们 contact @ memedata.com