你如何让git忽略目录的所有内容?

时间:2021-12-30 22:52:55

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目录,其中包含一大堆文件,然后有一个名为'sessions'的目录。 'sessions'包含我的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.

我需要文件夹'sessions'保留在git存储库中,因为没有该文件夹,程序无法正常运行。我不需要存储在git目录中的文件夹的实际内容。

So the question is:

所以问题是:

How can I get git to ignore the contents of a folder but not the folder itself?

如何让git忽略文件夹的内容而不是文件夹本身?

3 个解决方案

#1


If I'm remembering correctly, you can do this by creating a .gitignore file in the sessions folder with [^.]* as its contents.

如果我没记错,可以通过在[^。] *作为其内容的sessions文件夹中创建.gitignore文件来完成此操作。

#2


Add a sessions/.gitignore file with

添加一个sessions / .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文件,因此该文件夹不为空,但忽略其他所有内容。

#3


Since July 2007, gitignore does 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(这与pathpec在git中的工作方式一致)。

As illustrated by this thread, that pattern was not always expressed with a '/' for matching directory.

如该线程所示,该模式并不总是用匹配目录的'/'表示。

#1


If I'm remembering correctly, you can do this by creating a .gitignore file in the sessions folder with [^.]* as its contents.

如果我没记错,可以通过在[^。] *作为其内容的sessions文件夹中创建.gitignore文件来完成此操作。

#2


Add a sessions/.gitignore file with

添加一个sessions / .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文件,因此该文件夹不为空,但忽略其他所有内容。

#3


Since July 2007, gitignore does 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(这与pathpec在git中的工作方式一致)。

As illustrated by this thread, that pattern was not always expressed with a '/' for matching directory.

如该线程所示,该模式并不总是用匹配目录的'/'表示。