![](/img/trans.png)
[英]How do I ignore everything except a couple files in the top-level directory and everything in a few directories with .gitignore
[英]Make .gitignore ignore everything except a few files
我知道.gitignore
文件隱藏了 Git 版本控制中的指定文件。
我如何告訴.gitignore
忽略除我正在使用 Git 跟蹤的文件之外的所有內容? 就像是:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
一個可選的前綴
!
這否定了模式; 任何被先前模式排除的匹配文件都將再次包含在內。 如果否定模式匹配,這將覆蓋較低優先級的模式源。
# Ignore everything
*
# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...
# ...even if they are in subdirectories
!*/
# if the files to be tracked are in subdirectories
!*/a/b/file1.txt
!*/a/b/c/*
如果你想忽略一個目錄的全部內容,除了其中的一個文件,你可以為文件路徑中的每個目錄編寫一對規則。 例如.gitignore
忽略pippo
文件夾,除了pippo/pluto/paperino.xml
pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml
請注意,如果您只是在上面寫了:
pippo/*
!pippo/pluto/paperino.xml
它不起作用,因為中間的pluto
文件夾對 Git 不存在,所以paperino.xml
找不到存在的地方。
在大多數情況下,您想使用/*
而不是*
或*/
使用*
是有效的,但它以遞歸方式工作。 從那時起,它不會查看目錄。 人們建議使用!*/
再次包含目錄,但實際上最好使用/*
將最高級別的文件夾列入黑名單
# Blocklist files/folders in same directory as the .gitignore file
/*
# Includelist some files
!.gitignore
!README.md
# Ignore all files named .DS_Store or ending with .log
**/.DS_Store
**.log
# Includelist 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
上面的代碼將忽略除.gitignore
、 README.md
、 folder/a/file.txt
、 folder/a/b1/
和folder/a/b2/
以及最后兩個文件夾中包含的所有文件之外的所有文件。 (並且.DS_Store
和*.log
文件將在這些文件夾中被忽略。)
顯然,我也可以使用!/folder
或!/.gitignore
。
更多信息:http: //git-scm.com/docs/gitignore
更具體一點:
示例:忽略webroot/cache
中的所有內容 - 但保留webroot/cache/.htaccess
。
注意cache
文件夾后的斜線 (/):
失敗
webroot/cache*
!webroot/cache/.htaccess
作品
webroot/cache/*
!webroot/cache/.htaccess
# ignore these
*
# except foo
!foo
讓我們工具化!
正如@Joakim 所說,要忽略文件,您可以使用如下所示的內容。
# Ignore everything
*
# But not these files...
!.gitignore
!someFile.txt
但如果文件位於嵌套目錄中,則編寫這些規則有點困難。
例如,如果我們想跳過所有文件,但不跳過位於aDir/anotherDir/someOtherDir/aDir/bDir/cDir
中的a.txt
文件。 然后,我們的.gitignore
將是這樣的
# Skip all files
*
# But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
這很難手寫。
為了解決這個障礙,我創建了一個名為git-do-not-ignore的 Web 應用程序,它將為您生成規則。
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
要忽略目錄中的某些文件,您必須按正確的順序執行此操作:
例如,忽略文件夾“application”中除 index.php 和文件夾“config”之外的所有內容,注意順序。
你必須首先否定你想要的。
失敗
application/*
!application/config/*
!application/index.php
作品
!application/config/*
!application/index.php
application/*
有與 OP 類似的問題,但前 10 名贊成的答案都沒有真正起作用。
我終於發現了以下內容
錯誤的語法:
*
!bin/script.sh
正確的語法:
*
!bin
!bin/script.sh
來自gitignore 手冊頁的解釋:
可選前綴“!” 這否定了模式; 任何被先前模式排除的匹配文件都將再次包含在內。 如果該文件的父目錄被排除,則無法重新包含該文件。 出於性能原因,Git 不會列出排除的目錄,因此包含文件的任何模式都無效,無論它們是在哪里定義的。
這意味着上面的“錯誤語法”是錯誤的,因為bin/script.sh
不能重新包含為bin/
被忽略。 就這樣。
擴展示例:
$樹。
.
├── .gitignore
└── bin
├── ignore.txt
└── sub
└── folder
└── path
├── other.sh
└── script.sh
$ 貓 .gitignore
*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh
$ git status --untracked-files --ignored
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
bin/sub/folder/path/script.sh
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
bin/ignore.txt
bin/sub/folder/path/other.sh
nothing added to commit but untracked files present (use "git add" to track)
這就是我在忽略其他所有內容的同時保留文件夾結構的方式。 您必須在每個目錄(或 .gitkeep)中有一個 README.md 文件。
/data/*
!/data/README.md
!/data/input/
/data/input/*
!/data/input/README.md
!/data/output/
/data/output/*
!/data/output/README.md
您可以使用git config status.showUntrackedFiles no
並且所有未跟蹤的文件都將對您隱藏。 有關詳細信息,請參閱man git-config
。
要從 .gitignore 中排除文件夾,可以執行以下操作。
!app/
app/*
!app/bower_components/
app/bower_components/*
!app/bower_components/highcharts/
這將忽略bower_components
中的所有文件/子文件夾,除了/highcharts
。
有很多類似的問題,所以我將發布我之前寫的內容:
我讓它在我的機器上工作的唯一方法就是這樣做:
# 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
這些是有用的主題:
我也試過
*
*/*
**/**
和**/wp-content/themes/**
或/wp-content/themes/**/*
這些都不適合我。 很多線索和錯誤!
我是這樣做的:
# 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
您只會看到文件夾,這可能會讓您誤以為其中的所有內容都已被跟蹤
我的子文件夾有問題。
不工作:
/custom/*
!/custom/config/foo.yml.dist
作品:
/custom/config/*
!/custom/config/foo.yml.dist
我嘗試了上面給出的所有答案,但沒有一個對我有用。 閱讀 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/
否則它將無法按預期工作。
這對我有用,我只想向 repo 提交一個 Cordova 插件:
...
plugins/*
!plugins/cordova-plugin-app-customization
這里有很多復雜的答案......這是我在上面看不到的非常簡單的東西,並且在許多情況下都能很好地工作:
# ignore all files that have an extension
**/*.*
# except for these extensions
!**/*.extension1
!**/*.extension2
這不會忽略任何無擴展名文件,但如果您有一堆名稱相同或相似的文件,您可以為這些文件添加排除項
**/EXTENSIONLESSFILETOIGNORE
我有來自涼亭的 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
希望有人能發現這很有用。
如果您需要忽略除少數文件和少數根文件夾之外的所有內容,則簡單的解決方案:
/*
!.gitignore
!showMe.txt
!my_visible_dir
魔法就在/*
中(如上所述),它會忽略(根)文件夾中的所有內容,但不會遞歸。
我解決這個問題的最簡單方法是強制添加文件。 即使它被掩埋或嵌套在 git 忽略的子目錄樹中,它也會在 git 中進行說明。
例如:
x64 文件夾不包含在 .gitignore 中:
x64/
但是您想包含位於x64/Release/
目錄中的文件myFile.py
。 然后你必須:
git add -f x64/Release/myFile.py
您可以對匹配模式的多個文件執行此操作,例如
git add -f x64/Release/myFile*.py
等等。
我得到了這個工作
# Vendor
/vendor/braintree/braintree_php/*
!/vendor/braintree/braintree_php/lib
到目前為止,沒有什么對我有用,因為我試圖從 lib 中添加一個 jar。
這沒有用:
build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar
這有效:
build/*
!build/libs
我對單個文件的否定也有一些問題。 我能夠提交它們,但我的 IDE (IntelliJ) 總是抱怨被跟蹤的被忽略的文件。
git ls-files -i --exclude-from .gitignore
顯示了我以這種方式排除的兩個文件:
public/
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php
最后,這對我有用:
public/*
!public/typo3conf/
public/typo3conf/*
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php
關鍵是首先否定了typo3conf/
文件夾。
此外,似乎語句的順序無關緊要。 相反,您需要明確否定所有子文件夾,然后才能否定其中的單個文件。
文件夾!public/typo3conf/
和文件夾內容public/typo3conf/*
對於 .gitignore 來說是兩個不同的東西。
偉大的線程! 這個問題困擾了我一段時間;)
我似乎找到了其他人沒有提到的對我有用的東西。
# Ignore everything
*
# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...
# And if you want to include a sub-directory and all sub-directory and files under it, but not all sub-directories
!subdir/
!subdir/**/*
基本上,它似乎否定了一個子目錄被忽略,你必須有兩個條目,一個用於子目錄本身!subdir/
另一個用於擴展它下的所有文件和文件夾!subdir/**/*
這是一場災難,在使用git
16 年(2005 年)之后,當位於目錄層次結構的深處時,沒有簡單而明顯的方法來排除一個文件,否則應該被忽略。 相反,我們需要訴諸瘋狂的事情,例如反復挖掘結構並以正確的順序排除和包含目錄。 一年 4 次你需要做的事情是不可能記住的。
唯一聰明但非常奇怪的選擇是使用git add --force
。 當您不單獨處理 gitted repo 時,某些時候肯定會失敗。
所以我寫了一個小 bash 函數來為你解決這個問題,以便於復制粘貼。 讓我先解釋一下單線:
f=0; y='';for x in $(echo "uploads/rubbish/stuff/KEEP_ME/a.py" | tr '\/' '\n'); do y="$y/$x"; if [ $(($f)) -eq 0 ]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo;
# output:
!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a.py
描述
if
語句來調整第一個項目沒有得到/
。tr '\/' '\n'
- 將標准 POSIX 路徑中的/
轉換為\n
(以獲取列表)y="$y/$x"
- 在上一個目錄之后添加下一個子目錄(在列表中)。echo -e '!'"$y/\n$y/*"
-!
prefix ,第二個是/*
postfix 。sed '$d'
- 刪除最后一行sed -z 's/..$//'
- 從最后一行刪除最后 2 個字符/*
然后我們創建函數:
function gitkeep () { f=0; y='';for x in $(echo "$1" | tr '\/' '\n'); do y="$y/$x"; if [[ f -eq 0 ]]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo; }
# gitkeep "uploads/rubbish/stuff/KEEP_ME/a"
!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a
這里我將算術if
語句簡化為if [[ f -eq 0 ]];
.
享受!
# 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 # exclude everything except directory foo/bar /* !/foo /foo/* !/foo/bar
通過github.com/gitinclude,您可以下載一個命令行工具(適用於 windows、linux 和 osx),用於包含深度嵌套的文件集。
在.gitinclude
中定義文件集並執行該工具(通過雙擊或命令行)。 您的.gitignore
現已更新。
不確定是否已經指出了這一點,但我無法使用!
忽略忽略文件夾中的子文件夾。 在我希望 gitignore 忽略的文件夾前面。
但是我發現被忽略的文件夾在其路徑中沒有*
。 My.gitignore 看起來像這樣:
/folder/
!/folder/subfolder
子文件夾仍然被忽略,但是在文件夾之后添加*
使它像這樣工作
/folder/*
!/folder/subfolder
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.