使.gitignore忽略除少数文件之外的全部内容

我知道.gitignore文件会掩盖Git版本控制中的指定文件。 我有一个项目(LaTeX),它在运行时会生成许多其余文件(.auth,.dvi,.pdf,日志等),但我不但愿这些文件被跟踪。 php

我知道我能够(也许应该这样作)将全部这些文件放在项目的单独子文件夹中,由于这样我就能够忽略该文件夹了。 html

可是,是否有任何可行的方法将输出文件保留在项目树的根目录中,并使用.gitignore忽略除我正在使用Git跟踪的文件之外的全部内容? 就像是 jquery

# Ignore everything
*

# But not these files...
script.pl
template.latex
# etc...

#1楼

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

例如,忽略文件夹“ application”中的全部内容,但index.php和文件夹“ config”中要注意该顺序github

您必须否认要先想要的东西。 web

失败 app

application/* ide

!application/config/* this

!application/index.php spa

做品

!application/config/*

!application/index.php

application/*


#2楼

若是要忽略目录中除其中一个文件以外的所有内容,能够为文件路径中的每一个目录编写一对规则。 例如.gitignore忽略pippo文件夹,除了pippo / pluto / paperino.xml

.gitignore

pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml

#3楼

更具体一点:

示例:忽略webroot/cache全部内容-但保留webroot/cache/.htaccess

请注意cache文件夹后的斜杠(/):

失败

webroot/cache*
!webroot/cache/.htaccess

做品

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

#4楼

我有来自凉亭的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

但愿有人能发现这个有用。


#5楼

在大多数状况下,您想使用/*代替**/

使用*是有效的,可是它能够递归工做。 今后之后,它就再也不查找目录。 人们建议再次使用!*/将目录列入白名单,但最好使用/*将最高级别的文件夹列入黑名单

# Blacklist files/folders in same directory as the .gitignore file
/*

# Whitelist some files
!.gitignore
!README.md

# Ignore all files named .DS_Store or ending with .log
**/.DS_Store
**.log

# Whitelist folder/a/b1/ and folder/a/b2/
# trailing "/" is optional for folders, may match file though.
# "/" is NOT optional when followed by a *
!folder/
folder/*
!folder/a/
folder/a/*
!folder/a/b1/
!folder/a/b2/
!folder/a/file.txt

# Adding to the above, this also works...
!/folder/a/deeply
/folder/a/deeply/*
!/folder/a/deeply/nested
/folder/a/deeply/nested/*
!/folder/a/deeply/nested/subfolder

上面的代码将忽略.gitignoreREADME.mdfolder/a/file.txtfolder/a/b1/folder/a/b2/全部文件,以及后两个文件夹中的全部文件。 (而.DS_Store*.log文件将在这些文件夹中被忽略。)

显然,我也能够执行!/folder!/.gitignore

更多信息: http//git-scm.com/docs/gitignore

相关文章
相关标签/搜索