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

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

# Ignore everything:
*

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

当前回答

我是这样做的:

# Ignore everything
*

# Whitelist anything that's a directory
!*/

# Whitelist some files
!.gitignore

# Whitelist this folder and everything inside of it
!wordpress/wp-content/themes/my-theme/**

# Ignore this folder inside that folder
wordpress/wp-content/themes/my-theme/node_modules

# Ignore this file recursively
**/.DS_Store

使用gig status-u递归查看未跟踪目录中的单个文件-使用git status,您只能看到文件夹,这可能会让您误以为其中的所有内容都被跟踪

其他回答

更具体一点:

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

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

失败

webroot/cache*
!webroot/cache/.htaccess

作品

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

要从.gitignore中排除文件夹,可以执行以下操作。

!app/

app/*
!app/bower_components/

app/bower_components/*
!app/bower_components/highcharts/

这将忽略bower_components中的所有文件/子文件夹,/hightcharts除外。

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 these
*
# except foo
!foo

我有博沃的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

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