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

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

# Ignore everything:
*

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

当前回答

到目前为止,没有什么对我有用,因为我试图从lib中添加一个jar。

这不起作用:

build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar 

这是有效的:

build/*
!build/libs

其他回答

我把它搞定了

# Vendor
/vendor/braintree/braintree_php/*
!/vendor/braintree/braintree_php/lib

关于这一点,有很多类似的问题,所以我将发布我之前写的内容:

我在机器上实现这一点的唯一方法是这样做:

# 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内容/主题/**/*

这一切对我来说都不起作用。很多线索和错误!

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

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

你必须先否定你想要的。

失败

应用程序/*

!应用程序/配置/*

!应用程序/index.php

作品

!应用程序/配置/*

!应用程序/index.php

应用程序/*

我尝试了上面给出的所有答案,但没有一个对我有效。在阅读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/

否则它将无法按预期工作。

更具体一点:

示例:忽略webroot/cache中的所有内容,但保留webroot/cache/.htaccess。

请注意缓存文件夹后面的斜杠(/):

失败

webroot/cache*
!webroot/cache/.htaccess

作品

webroot/cache/*
!webroot/cache/.htaccess