我試圖從 git 存盤庫中排除除.asd檔案之外的所有內容。檔案可能位于目錄層次結構的任何級別,任何級別的任何目錄的名稱中可能包含也可能不包含點,使它們看起來像檔案名。
例如:
設定存盤庫和一些目錄結構:
$ mkdir repository && cd repository && git init && touch .gitignore
$ mkdir -p foo/fooo/foooo/fooooo.srcs/fooooo bar/barr.srcs/barr/barrr foo/fooo/foooo/fooooo.cache/fooooo
.asd在樹中的各個級別添加一些我們想要包含的檔案:
$ touch foo/file1.asd foo/fooo/foooo/fooooo.srcs/file2.asd bar/barr.srcs/file3.asd bar/barr.srcs/barr/barrr/file4.asd foo/fooo/file5.asd
托管存盤庫的父目錄的大部分內容是隨機的。這些東西可能是各種未知型別的檔案等,因此特別宣告應該忽略的所有內容是不可取的。宣告我們想要包含的內容更容易(或應該更容易)。添加一些隨機的東西:
$ touch huge.pdf asd.yo foo/fooo/foooo/fooooo.cache/foo.txt foo/fooo/foooo/fooooo.cache/fooooo/asd.sh bar/intro.pptx
使用空的 .gitignore,所有內容都包含在內:
$ git status -u -s
?? .gitignore
?? asd.yo
?? bar/barr.srcs/barr/barrr/file4.asd
?? bar/barr.srcs/file3.asd
?? bar/intro.pptx
?? foo/file1.asd
?? foo/fooo/file5.asd
?? foo/fooo/foooo/fooooo.cache/foo.txt
?? foo/fooo/foooo/fooooo.cache/fooooo/asd.sh
?? foo/fooo/foooo/fooooo.srcs/file2.asd
?? huge.pdf
我嘗試過以下方法.gitignore:
*.*
!*/**/*.asd
它只包含file1.asd和file5.asd缺少.asd某些目錄中的其他 3 個檔案,這些檔案的名稱中包含點。嘗試專門添加其中之一揭示了問題:
$ git add foo/fooo/foooo/fooooo.srcs/file2.asd
The following paths are ignored by one of your .gitignore files:
foo/fooo/foooo/fooooo.srcs
Use -f if you really want to add them.
那么是否有可能只看到.asd僅.gitignore在這種存盤庫結構中使用的檔案?畢竟這里追求的東西更像是一個“.gitinclude”的東西,我猜它不存在。當然,我可以忘記嘗試定義相關的 git 范圍.gitignore,而只是使用git add *.asd代替,git add .但這不是很好。例如,VS Code 的 git 擴展名過于豐富多彩,因為它也考慮了 git 范圍內的所有不相關檔案。
uj5u.com熱心網友回復:
要打敗 Git 的.gitignore目錄優化,請使用:
!*/
在這種情況下,您最終會得到這個.gitignore檔案:
*
!*/
!*.asd
請注意,這意味著 eachgit status將搜索您作業樹中的每個目錄,無論這是否最終毫無意義。所以它有點 CPU 昂貴——但它會解決這個特定的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/313218.html
上一篇:在AzureDevops中創建分支時對Author的澄清
下一篇:如何診斷git提交歷史中的問題
