我知道.gitignore文件隐藏Git版本控制中的指定文件。

我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:

# Ignore everything:
*

# Do not ignore these files:
script.pl
template.latex

当前回答

要忽略目录中的某些文件,必须按正确的顺序执行此操作:

例如,忽略文件夹“application”中除index.php和文件夹“config”外的所有内容,注意顺序。

你必须先否定你想要的。

失败

应用程序/*

!应用程序/配置/*

!应用程序/index.php

作品

!应用程序/配置/*

!应用程序/index.php

应用程序/*

其他回答

最简单的方法是强制添加文件。即使它被隐藏或嵌套在git忽略的子目录树中,它也将在git中进行说明。

例如:

x64文件夹已在中排除。gitignore:

x64/

但您希望包含位于x64/发布/目录中的文件myFile.py。然后你必须:

git add -f x64/Release/myFile.py

您可以对匹配模式的多个文件执行此操作,例如。

git add -f x64/Release/myFile*.py

等等

要从.gitignore中排除文件夹,可以执行以下操作。

!app/

app/*
!app/bower_components/

app/bower_components/*
!app/bower_components/highcharts/

这将忽略bower_components中的所有文件/子文件夹,/hightcharts除外。

这就是我保持文件夹结构的方式,而忽略其他所有内容。每个目录(或.gitkeep)中都必须有一个README.md文件。

/data/*
!/data/README.md

!/data/input/
/data/input/*
!/data/input/README.md

!/data/output/
/data/output/*
!/data/output/README.md

不确定是否已经指出了这一点,但我在使用!在我希望gitignore忽略的文件夹前面。

然而,我发现被忽略的文件夹路径中没有*。我的gitignore看起来像这样:

/folder/
!/folder/subfolder

子文件夹仍被忽略,但在文件夹后添加一个*使其工作正常

/folder/*
!/folder/subfolder

要忽略目录中的某些文件,必须按正确的顺序执行此操作:

例如,忽略文件夹“application”中除index.php和文件夹“config”外的所有内容,注意顺序。

你必须先否定你想要的。

失败

应用程序/*

!应用程序/配置/*

!应用程序/index.php

作品

!应用程序/配置/*

!应用程序/index.php

应用程序/*