我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
我知道.gitignore文件隐藏Git版本控制中的指定文件。
我如何告诉.gitignore忽略除我使用Git跟踪的文件之外的所有内容?类似于:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
当前回答
这里有很多复杂的答案。。。这里有一些我在上面看不到的非常简单的东西,在许多场景中都很有效:
# ignore all files that have an extension
**/*.*
# except for these extensions
!**/*.extension1
!**/*.extension2
这不会忽略任何无扩展名的文件,但是如果您有一堆文件都具有相同或相似的名称,则可以为这些文件添加排除项
**/EXTENSIONLESSFILETOIGNORE
其他回答
# ignore these
*
# except foo
!foo
不确定是否已经指出了这一点,但我在使用!在我希望gitignore忽略的文件夹前面。
然而,我发现被忽略的文件夹路径中没有*。我的gitignore看起来像这样:
/folder/
!/folder/subfolder
子文件夹仍被忽略,但在文件夹后添加一个*使其工作正常
/folder/*
!/folder/subfolder
要从.gitignore中排除文件夹,可以执行以下操作。
!app/
app/*
!app/bower_components/
app/bower_components/*
!app/bower_components/highcharts/
这将忽略bower_components中的所有文件/子文件夹,/hightcharts除外。
我对否定单个文件也有一些问题。我能够提交它们,但我的IDE(IntelliJ)总是抱怨被忽略的文件被跟踪。
git ls-files -i --exclude-from .gitignore
显示了两个文件,我以这种方式排除了这两个文件:
public/
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php
最后,这对我有用:
public/*
!public/typo3conf/
public/typo3conf/*
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php
关键是对文件夹typeo3conf/first的否定。
此外,声明的顺序似乎无关紧要。相反,您需要显式否定所有子文件夹,然后才能否定其中的单个文件。
文件夹!public/type3conf/和文件夹内容public/pype3conf/*是.gitignore的两个不同的东西。
伟大的线程!这个问题困扰了我一段时间;)
可选前缀!这否定了该模式;排除的任何匹配文件将再次包括先前的模式。如果否定模式匹配,这将覆盖优先级较低的模式源。
# Ignore everything
*
# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...
# ...even if they are in subdirectories
!*/
# if the files to be tracked are in subdirectories
!*/a/b/file1.txt
!*/a/b/c/*