.gitignore Isn’t the Only Way To Ignore Files in Git
.gitignore Isn’t the Only Way To Ignore Files in Git
.gitignore 并不是在 Git 中忽略文件的唯一方式
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, and ~/.config/git/ignore.
我使用 Git 已经很久了,最近才意识到除了 .gitignore 之外,还可以在三个不同的层级上忽略文件。你可以用来忽略文件的三个文件分别是:.gitignore、.git/info/exclude 和 ~/.config/git/ignore。
.gitignore
.gitignore
.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.
.gitignore 是我们通常用来写入想要忽略文件的文件。它会随代码库的其他部分一起提交到 Git 中。添加到该文件中的任何文件在运行 Git 命令时都会被忽略。
.git/info/exclude
.git/info/exclude
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.
exclude 文件位于每个 Git 仓库的 .git 目录下,但对它的修改不会被提交到 Git 中。在新建的 Git 仓库中,它通常包含几行注释。这个文件非常适合用于针对特定仓库进行忽略设置。例如,你可能在某个仓库中有一个个人的 notes.txt 文件,你不想将其提交到 Git,但又不想把它添加到 .gitignore 中(因为它只属于你个人的工作流)。在这种情况下,你可以将 notes.txt 添加到 .git/info/exclude 中。
~/.config/git/ignore
~/.config/git/ignore
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.
ignore 文件位于你机器的主目录下,路径为 ~/.config/git/ignore。添加到此文件中的任何文件名都会在机器级别被全局忽略。该文件不会被提交到 Git,也不与任何特定仓库关联。如果你希望在电脑上的每个 Git 仓库中都忽略某些文件,这里是最佳位置。例如,如果你使用的是 macOS,将 .DS_Store 添加到这里就非常合适。
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:
你可以自定义全局忽略文件,将其设置为其他文件。例如,如果你希望全局 Git 忽略文件为 .gitignore_global,可以运行以下命令:
git config --global core.excludesFile ~/.gitignore_global
And if you ever want to return to the default setting, run:
如果你想恢复到默认设置,请运行:
git config --global --unset core.excludesFile
Checking Which File Is Ignoring a Specific File
查看是哪个文件在忽略特定文件
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.
当向上述任何文件添加文件名时,你可以使用此命令来检查该文件名是如何被忽略的。例如,如果你想检查 .DS_Store 是如何被忽略的,可以在任何 Git 仓库中运行 git check-ignore -v .DS_Store。
Here is the output when the repository’s .gitignore is ignoring .DS_Store:
以下是当仓库的 .gitignore 忽略 .DS_Store 时的输出:
$ 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:
以下是当仓库的 .git/info/exclude 忽略 .DS_Store 时的输出:
$ 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:
以下是当全局 ~/.config/git/ignore 文件忽略 .DS_Store 时的输出:
$ 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:
以下是当自定义全局忽略文件 .gitignore_global 忽略 .DS_Store 时的输出:
$ 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.
如果没有文件在忽略某个特定文件,git check-ignore -v 命令将不会产生任何输出。
This post received a lot of attention on Hacker News! Check it out here: https://news.ycombinator.com/item?id=48583356 这篇文章在 Hacker News 上受到了广泛关注!点击此处查看:https://news.ycombinator.com/item?id=48583356