问题
Suppose I have the following git repository structure
> tree -a
.
├── .git
│ ├── info
│ │ └── exclude
└── a
├── .gitignore
├── mustinclude.txt
└── my-local-file-to-ignore.txt
And the gitignore file says to always track txt files
> cat a/.gitignore
*
!*.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.
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
回答1:
.gitignore
has a higher priority over $GIT_DIR/info/exclude
.
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.
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.Patterns read from
$GIT_DIR/info/exclude
.Patterns read from the file specified by the configuration variable
core.excludesFile
.
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.
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.
来源:https://stackoverflow.com/questions/58218550/override-subdirectory-gitignores-must-include-rule-with-local-git-info-exclu