你如何让 git 忽略目录的所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/482309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you get git to ignore all contents of a directory?
提问by Tabitha
I have a git directory which contains the a whole bunch of files and then has a directory called 'sessions'. 'sessions' contains cookie information for my web.py program.
我有一个 git 目录,其中包含一大堆文件,然后有一个名为“会话”的目录。“会话”包含我的 web.py 程序的 cookie 信息。
I need the folder 'sessions' to remain in the git repository because without the folder the program does not function correctly. I don't need the actual contents of folder being stored in the git directory.
我需要将文件夹“会话”保留在 git 存储库中,因为没有该文件夹,程序将无法正常运行。我不需要将文件夹的实际内容存储在 git 目录中。
So the question is:
所以问题是:
How can I get git to ignore the contents of a folder but not the folder itself?
如何让 git 忽略文件夹的内容而不是文件夹本身?
采纳答案by ceejayoz
If I'm remembering correctly, you can do this by creating a .gitignore
file in the sessions
folder with [^.]*
as its contents.
如果我没记错的话,您可以通过.gitignore
在sessions
文件夹中创建一个文件[^.]*
作为其内容来做到这一点。
回答by Mathias V
Add a sessions/.gitignore file with
添加一个会话/.gitignore 文件
*
!.gitignore
The second line tells git not to ignore the .gitignore file, so the folder is not empty but everything else is ignored.
第二行告诉 git 不要忽略 .gitignore 文件,因此该文件夹不是空的,但其他所有内容都将被忽略。
回答by VonC
Since July 2007, gitignoredoes describe the exclusion patterns.
自2007年7 月以来,gitignore确实描述了排除模式。
If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory.
In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo(this is consistent with the way how pathspec works in general in git).
如果模式以斜杠结尾,则出于以下描述的目的将其删除,但它只会找到与目录 匹配的内容。
换句话说, foo/ 将匹配目录 foo 和它下面的路径,但不会匹配常规文件或符号链接 foo(这与 pathspec 在 git 中的一般工作方式一致)。
As illustrated by this thread, that pattern was not always expressed with a '/' for matching directory.
如该线程所示,该模式并不总是用“/”来表示匹配目录。