.gitignore排除规则实际上如何工作? [英] How do .gitignore exclusion rules actually work?
问题描述
我正在尝试解决大型目录结构上的gitignore问题,但是为了简化我的问题,我将其简化为以下内容.
I'm trying to solve a gitignore problem on a large directory structure, but to simplify my question I have reduced it to the following.
在全新的git存储库中,我具有以下两个文件(foo,bar)的目录结构(到目前为止尚未提交):
I have the following directory structure of two files (foo, bar) in a brand new git repository (no commits so far):
a/b/c/foo
a/b/c/bar
很明显,'git status -u'显示:
Obviously, a 'git status -u' shows:
# Untracked files:
...
# a/b/c/bar
# a/b/c/foo
我想做的是创建一个.gitignore文件,该文件忽略a/b/c内部的所有内容,但不忽略文件'foo'.
What I want to do is create a .gitignore file that ignores everything inside a/b/c but does not ignore the file 'foo'.
如果我这样创建.gitignore:
If I create a .gitignore thus:
c/
然后'git status -u'显示foo和bar被忽略:
Then a 'git status -u' shows both foo and bar as ignored:
# Untracked files:
...
# .gitignore
那是我所期望的.
现在,如果我为foo添加一个排除规则,则:
Now if I add an exclusion rule for foo, thus:
c/
!foo
根据gitignore联机帮助页,我希望它可以正常工作.但这不是-它仍然忽略foo:
According to the gitignore manpage, I'd expect this to to work. But it doesn't - it still ignores foo:
# Untracked files:
...
# .gitignore
这也不起作用:
c/
!a/b/c/foo
这也不是:
c/*
!foo
赠予:
# Untracked files:
...
# .gitignore
# a/b/c/bar
# a/b/c/foo
在这种情况下,尽管不再忽略foo,但也不忽略bar.
In that case, although foo is no longer ignored, bar is also not ignored.
.gitignore中规则的顺序似乎也无关紧要.
The order of the rules in .gitignore doesn't seem to matter either.
这也没有达到我的期望:
This also doesn't do what I'd expect:
a/b/c/
!a/b/c/foo
那个人忽略foo和bar.
That one ignores both foo and bar.
一种可行的情况是,如果我创建文件a/b/c/.gitignore并将其放在其中:
One situation that does work is if I create the file a/b/c/.gitignore and put in there:
*
!foo
但是问题是最终在a/b/c下还会有其他子目录,我不想将每个单独的.gitignore放到每个子目录中-我希望创建基于项目的..gitignore文件可以位于每个项目的顶层目录中,并涵盖所有标准"子目录结构.
But the problem with this is that eventually there will be other subdirectories under a/b/c and I don't want to have to put a separate .gitignore into every single one - I was hoping to create 'project-based' .gitignore files that can sit in the top directory of each project, and cover all the 'standard' subdirectory structure.
这似乎也等效:
a/b/c/*
!a/b/c/foo
这可能是我可以实现的与工作"最接近的事情,但是需要说明完整的相对路径和显式异常,如果我有很多名为"foo"的文件,这将是一件痛苦的事情在子目录树的不同级别.
This might be the closest thing to "working" that I can achieve, but the full relative paths and explicit exceptions need to be stated, which is going to be a pain if I have a lot of files of name 'foo' in different levels of the subdirectory tree.
无论如何,要么我不太了解排除规则的工作原理,要么忽略目录(而不是通配符)时它们根本不起作用-规则以/
Anyway, either I don't quite understand how exclusion rules work, or they don't work at all when directories (rather than wildcards) are ignored - by a rule ending in a /
任何人都可以阐明这一点吗?
Can anyone please shed some light on this?
是否有一种方法可以使gitignore使用诸如正则表达式之类的明智方法,而不是使用这种笨拙的基于shell的语法?
Is there a way to make gitignore use something sensible like regular expressions instead of this clumsy shell-based syntax?
我正在使用,并在Cygwin/bash3上的git-1.6.6.1和在Ubuntu/bash3上的git-1.7.1进行观察.
I'm using and observe this with git-1.6.6.1 on Cygwin/bash3 and git-1.7.1 on Ubuntu/bash3.
推荐答案
/a/b/c/*
!foo
似乎对我有用(Linux上为git 1.7.0.4). *
很重要,因为否则您将忽略目录本身(因此git不会进入内部),而不是目录中的文件(允许排除).
Seems to work for me (git 1.7.0.4 on Linux). The *
is important as otherwise you're ignoring the directory itself (so git won't look inside) instead of the files within the directory (which allows for the exclusion).
认为排除项是说但不是这个"而不是但包括这个"-忽略此目录(/a/b/c/
)而不是这个目录(foo
)"没有多大意义; 忽略此目录(/a/b/c/*
)中的所有文件,但忽略此目录(foo
)".引用手册页:
Think of the exclusions as saying "but not this one" rather than "but include this" - "ignore this directory (/a/b/c/
) but not this one (foo
)" doesn't make much sense; "ignore all files in this directory (/a/b/c/*
) but not this one (foo
)" does. To quote the man page:
一个可选的前缀!否定了模式;先前模式排除的所有匹配文件都将再次包含在内.
An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again.
,即文件必须已经排除在外,才能再次包含.希望能有所启发.
i.e., the file has to have been excluded already to be included again. Hope that sheds some light.
这篇关于.gitignore排除规则实际上如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!