我知道.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
当前回答
我尝试了上面给出的所有答案,但没有一个对我有效。在阅读gitignore文档(此处)后,我发现如果首先排除文件夹,则子文件夹中的文件名不会被索引。因此,如果您随后使用感叹号来包含文件,则在索引中找不到该文件,因此不会包含在git客户端中。
这就是找到解决方案的方法。我开始为我的文件夹树中的所有子文件夹添加例外以使其正常工作,这是一项艰巨的工作。之后,我能够将详细配置压缩到下面的配置,这有点与文档相反。。
正在工作.gitignore:
# Ignore the 'Pro' folder, except for the '3rdparty' subfolder
/Pro/*
!Pro/3rdparty/
# Ignore the '3rdparty' folder, except for the 'domain' subfolder
/Pro/3rdparty/*
!Pro/3rdparty/domain/
# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/
结果,我在我的git客户端中看到,只有Pro/3rdparty/domain/modulename/文件夹中的两个文件正在准备下一次提交,而这正是我想要的。
如果您需要将同一文件夹的多个子文件夹列入白名单,请将exclude语句下面的感叹号行分组,如下所示:
# Ignore the 'Pro' folder, except for the '3rdparty' subfolder
/Pro/*
!Pro/3rdparty/
# Ignore the '3rdparty' folder, except for the 'domain' & 'hosting' subfolders
/Pro/3rdparty/*
!Pro/3rdparty/domain/
!Pro/3rdparty/hosting/
# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/
# Ignore the 'hosting' folder, except for the 'modulename' subfolder
Pro/3rdparty/hosting/*
!Pro/3rdparty/hosting/modulename/
否则它将无法按预期工作。
其他回答
Gist
# Ignore everything
*
# But not these files...
!script.pl
!template.latex
可能包括:
!.gitignore
参考
从…起https://git-scm.com/docs/gitignore:
一个可选前缀“!”,用于否定模式;被先前模式排除的任何匹配文件将再次包含。如果排除了文件的父目录,则无法重新包含该文件。由于性能原因,Git没有列出排除的目录,因此包含的文件上的任何模式都不会产生任何影响,无论它们是在哪里定义的。对于以文字“!”开头的模式,在第一个“!”前面加一个反斜杠(“\”),例如“\!important!.txt”。...排除除特定目录foo/bar以外的所有内容的示例(注意/*-不带斜杠,通配符也会排除foo/bar内的所有内容):$cat.gitignore#排除目录foo/bar以外的所有内容/*!/食品/食品/*!/foo/条
这里有很多复杂的答案。。。这里有一些我在上面看不到的非常简单的东西,在许多场景中都很有效:
# ignore all files that have an extension
**/*.*
# except for these extensions
!**/*.extension1
!**/*.extension2
这不会忽略任何无扩展名的文件,但是如果您有一堆文件都具有相同或相似的名称,则可以为这些文件添加排除项
**/EXTENSIONLESSFILETOIGNORE
如果您想忽略目录中除一个文件外的全部内容,可以为文件路径中的每个目录编写一对规则。例如,.gitignore将忽略pippo文件夹,pippo/pluto/paperino.xml除外
pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml
请注意,如果您只是在上面写了:
pippo/*
!pippo/pluto/paperino.xml
它无法工作,因为Git中不存在中间的pluto文件夹,因此paperino.xml找不到存在的位置。
关于这一点,有很多类似的问题,所以我将发布我之前写的内容:
我在机器上实现这一点的唯一方法是这样做:
# Ignore all directories, and all sub-directories, and it's contents:
*/*
#Now ignore all files in the current directory
#(This fails to ignore files without a ".", for example
#'file.txt' works, but
#'file' doesn't):
*.*
#Only Include these specific directories and subdirectories and files if you wish:
!wordpress/somefile.jpg
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/
!wordpress/*/wp-content/themes/*
!wordpress/*/wp-content/themes/*/*
!wordpress/*/wp-content/themes/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*
!wordpress/*/wp-content/themes/*/*/*/*/*
请注意,您必须明确允许包含每个级别的内容。所以如果我在主题下有5个子目录,我仍然需要把它拼出来。
这是@Yarin在这里的评论:https://stackoverflow.com/a/5250314/1696153
这些是有用的主题:
否定模式如何在.gitignore中工作?gitignore排除规则实际上是如何工作的?
我也试过了
*
*/*
**/**
和**/wp内容/主题/**
或/wp内容/主题/**/*
这一切对我来说都不起作用。很多线索和错误!
要从.gitignore中排除文件夹,可以执行以下操作。
!app/
app/*
!app/bower_components/
app/bower_components/*
!app/bower_components/highcharts/
这将忽略bower_components中的所有文件/子文件夹,/hightcharts除外。