使.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

相關文章
相關標籤/搜索