Git忽略目录和目录/ *有什么区别? [英] What's the difference between Git ignoring directory and directory/*?
问题描述
假设我有以下目录结构:
my_project
| --www
| --1.txt
| --2.txt
| - 。。gitignore
这个之间有什么区别:
www
www / *
我问这个问题的原因是:在git中,如果一个目录是空的,那么git不会在repository中包含这样的空目录。所以我试着在目录下添加一个额外的.gitkeep文件,这样它就不会是空的解决方案。当我尝试这种解决方案时,如果在.gitignore文件中,我写下如下:
www
!* .gitkeep
它不起作用(我的目的是忽略www下的所有内容,但保留目录)。但如果我尝试以下操作:
www / *
!*。gitkeep
然后它就起作用了!所以我认为这两种方法之间必须有一些区别。 > , www /
和 www / *
。
基本上来自文档和我自己的测试, www
找到与文件或目录的匹配项, www /
只匹配一个目录,而 www / *
匹配 www
中的目录和文件。
我会只讨论 www /
和 www / *
之间的区别,因为 www
和 www /
是显而易见的。 $ b
对于 www /
,git忽略目录 www
本身,这意味着git甚至不会在里面看到。但是对于 www / *
,git会检查 www
内的所有文件/文件夹,并忽略模式 *
。它似乎导致相同的结果,因为如果忽略所有子文件/文件夹,git将不会跟踪空文件夹 www
。事实上,OP的结果与 www /
或 www / *
独立无关。但是,如果与其他规则相结合,它确实会产生差异。例如,如果我们只想包含 www / 1.txt
但忽略<$>中的所有其他内容c $ c> www ?
下面的 .gitignore
不起作用。
www /
!www / 1.txt
尽管以下 .gitignore
有效,为什么?
www / *
!www / 1.txt
对于前者,git只是忽略了目录 www
,甚至不会在内部包含 www / 1.txt
再次。第一条规则排除了父目录 www
但不是 www / 1.txt
,结果 www / 1.txt
不能为再次包含。
但是对于后者,git首先会忽略 www
下的所有文件/作弊者,然后再包括其中一个它是 www / 1.txt
。
在这个例子中,文档中的以下几行可以帮助:
可选的前缀!否定了这种模式;任何匹配文件
将被重新包括在内。如果该文件的父目录不包含
,那么重新包含文件不是
。
I'm confused about what's the correct way to ignore the contents of a directory in git.
Assume I have the following directory structure:
my_project
|--www
|--1.txt
|--2.txt
|--.gitignore
What's the difference between putting this:
www
And this?
www/*
The reason I'm asking this question is: In git, if a directory is empty, git won't include such empty directory in repository. So I was trying the solution that is add an extra .gitkeep file under the directory so that it won't be empty. When I was trying that solution, if in the .gitignore file, I write like below:
www
!*.gitkeep
It doesn't work(My intention is to ignore all contents under www but keep the directory). But if I try the following:
www/*
!*.gitkeep
Then it works! So I think it must has some differences between the two approaches.
There're differences among www
, www/
and www/*
.
Basically from the documentation and my own tests, www
find a match with a file or a directory, www/
only matches a directory, while www/*
matches directories and files inside www
.
I'll only discuss on the differences between www/
and www/*
here, since the differences between www
and www/
are obvious.
For www/
, git ignores the directory www
itself, which means git won't even look inside. But for www/*
, git checks all files/folders inside www
, and ignores all of them with the pattern *
. It seems to lead to the same results since git won't track an empty folder www
if all its child files/folders are ignored. And indeed the results will be no difference for OP's case with www/
or www/*
standalone. But it does make differences if it's combined with other rules.
For example, what if we want to only include www/1.txt
but ignore all others inside www
?
The following .gitignore
won't work.
www/
!www/1.txt
While the following .gitignore
works, why?
www/*
!www/1.txt
For the former, git just ignores the directory www
, and won't even look inside to include www/1.txt
again. The first rule excludes the parent directory www
but not www/1.txt
, and as a result www/1.txt
cannot be "included again".
But for the latter, git first ignores all files/folers under www
, and then includes one of them again which is www/1.txt
.
For this example, the follwing lines in the documentation may help:
An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded.
这篇关于Git忽略目录和目录/ *有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!