我知道.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 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/*
其他回答
关于这一点,有很多类似的问题,所以我将发布我之前写的内容:
我在机器上实现这一点的唯一方法是这样做:
# 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内容/主题/**/*
这一切对我来说都不起作用。很多线索和错误!
更具体一点:
示例:忽略webroot/cache中的所有内容,但保留webroot/cache/.htaccess。
请注意缓存文件夹后面的斜杠(/):
失败
webroot/cache*
!webroot/cache/.htaccess
作品
webroot/cache/*
!webroot/cache/.htaccess
我尝试了上面给出的所有答案,但没有一个对我有效。在阅读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/
否则它将无法按预期工作。
让我们工具化!
正如@Joakim所说,要忽略文件,可以使用下面的方法。
# Ignore everything
*
# But not these files...
!.gitignore
!someFile.txt
但是如果文件位于嵌套目录中,那么编写这些规则有点困难。
例如,如果我们希望跳过所有文件,但不跳过位于aDir/aotherDir/someOtherDir/aDir/bDir/cDir中的.txt文件。那么,我们的.gitignore将是这样的
# Skip all files
*
# But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
这很难用手写。
为了解决这个问题,我创建了一个名为git do not ignore的web应用程序,它将为您生成规则。
Tool
https://theapache64.github.io/git-do-not-ignore/
Demo
样本输入
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
样本输出
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
您可以使用git-config-status.showUntrackedFiles no,所有未跟踪的文件都将对您隐藏。有关详细信息,请参阅man git config。