覆盖子目录gitignore的“必须包括"本地.git/info/exclude的规则 [英] Override subdirectory gitignore's "must include" rule with local .git/info/exclude

查看:142
本文介绍了覆盖子目录gitignore的“必须包括"本地.git/info/exclude的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我具有以下git存储库结构

Suppose I have the following git repository structure

> tree -a
.
├── .git
│   ├── info
│   │   └── exclude
└── a
    ├── .gitignore
    ├── mustinclude.txt
    └── my-local-file-to-ignore.txt

并且gitignore文件说总是跟踪txt文件

And the gitignore file says to always track txt files

> cat a/.gitignore
*
!*.txt

是否可以配置git,使其仅为我自己忽略 a/my-local-file-to-ignore.txt ?这样做的动机是我想包括本地暂存文件(用于测试或进行其他操作),但我不希望其他使用存储库的人知道此类文件的存在.

Is it possible to configure git so that it will ignore a/my-local-file-to-ignore.txt just for myself? The motivation for this is I want to include local scratch files (for testing or whatever) but I don't want other people using the repository to know about the existence of such a file.

我尝试将该文件放入info/exclude文件中,但是它不起作用.我认为这是因为a中的gitignore文件始终包含txt文件.

I have tried putting that file in the info/exclude file but it doesn't work. I think this is because the gitignore file in a specifies to always include txt file.

> cat .git/info/exclude
a/my-local-file-to-ignore.txt

> git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   mustinclude.txt
    new file:   my-local-file-to-ignore.txt

推荐答案

.gitignore 的优先级高于 $ GIT_DIR/info/exclude .

Git通常会从多个来源检查 gitignore 模式,并按照以下优先顺序,从最高到最低(在一个优先级内),最后一个匹配模式决定结果):

Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome):

  • 从命令行读取支持这些命令的模式.

  • Patterns read from the command line for those commands that support them.

模式,其模式在较高级别的文件(直到工作树的最高级别)中)被较低级别文件中的文件覆盖,直到包含该文件的目录.这些模式相对于 .gitignore 文件的位置匹配.一个项目通常在其存储库中包含这样的 .gitignore 文件,其中包含作为项目构建的一部分而生成的文件的模式.

Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file. These patterns match relative to the location of the .gitignore file. A project normally includes such .gitignore files in its repository, containing patterns for files generated as part of the project build.

$ GIT_DIR/info/exclude 中读取的模式.

从配置变量 core.excludesFile 指定的文件中读取模式.

Patterns read from the file specified by the configuration variable core.excludesFile.

根据 gitignore 手册. $ GIT_DIR/info/exclude 是仅想在本地忽略文件时应使用的文件.问题是您的 a/.gitignore 中的模式过度匹配.

According to gitignore manual. $GIT_DIR/info/exclude is the file you should use when you wanna ignore files locally for yourself only. The problem is that the patterns in your a/.gitignore overmatch.

要实现所需的功能,您需要从 a/.gitignore 中删除全局忽略模式 * ,并仅忽略那些不需要的文件.

To achieve what your want, you need to remove the global ignore patter * from a/.gitignore and ignore ONLY those files you don't want.

这篇关于覆盖子目录gitignore的“必须包括"本地.git/info/exclude的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆