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

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

# Ignore everything:
*

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

当前回答

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

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

# 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

我的子文件夹有问题。

不工作:

/custom/*
!/custom/config/foo.yml.dist

作品:

/custom/config/*
!/custom/config/foo.yml.dist

这里有很多复杂的答案。。。这里有一些我在上面看不到的非常简单的东西,在许多场景中都很有效:

# ignore all files that have an extension
**/*.*

# except for these extensions
!**/*.extension1
!**/*.extension2

这不会忽略任何无扩展名的文件,但是如果您有一堆文件都具有相同或相似的名称,则可以为这些文件添加排除项

**/EXTENSIONLESSFILETOIGNORE

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

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

你必须先否定你想要的。

失败

应用程序/*

!应用程序/配置/*

!应用程序/index.php

作品

!应用程序/配置/*

!应用程序/index.php

应用程序/*

我有博沃的Jquery和Angular。Bower将其安装在

/public_html/bower_components/jquery/dist/bunch-of-jquery-files
/public_html/bower_components/jquery/src/bunch-of-jquery-source-files
/public_html/bower_components/angular/angular-files

最小化的jquery位于dist目录中,angular位于angular目录中。我只需要最小化的文件就可以提交到github。对gitignore进行了一些篡改,这就是我设法变出来的。。。

/public_html/bower_components/jquery/*
!public_html/bower_components/jquery/dist
/public_html/bower_components/jquery/dist/*
!public_html/bower_components/jquery/dist/jquery.min.js
/public_html/bower_components/angular/*
!public_html/bower_components/angular/angular.min.js

希望有人能发现这个有用。