[英]How do I ignore everything except a couple files in the top-level directory and everything in a few directories with .gitignore
[英]How can I write a rule in gitignore to ignore all files except directories in some directory?
所以我要說我有一個像這樣的目錄結構
uploads/
--dir1/
----one.txt
----two.txt
----dir2/
------one.txt
------two.txt
我希望跟蹤上傳的任何目錄和子目錄,但不能跟蹤其中的任何文件,除了虛擬文件,因為git不跟蹤目錄。
我認為這樣的事情會奏效,但事實並非如此。
*
!.gitignore
當你告訴它忽略*
,Git不會遞歸到任何子目錄中(即使你有一個“unignore”模式可能與其中一個目錄內的東西相匹配)。
有一些gitignore模式語言可能會有所幫助。 尾部斜杠強制模式僅應用於目錄。 所以,你應該可以使用這個:
# .gitignore
*
!.keep
!*/
在下面的演示中,我使用上面的.gitignore
並在每個.keep
文件旁邊有一個額外的文件(名為do-not-keep
)。 您可以看到它適用於多個子目錄級別,並且不顯示其他文件。 您必須通過一些獨立的方法安排每個目錄擁有自己的.keep
(或其他)。
% git status --short -uall
?? a/.keep
?? a/b/.keep
?? a/b/c/.keep
?? a/b/c/d/.keep
?? e/.keep
?? e/f/.keep
?? e/f/g/.keep
?? h/.keep
?? h/i/.keep
?? j/.keep
這是通過Git 1.7.3.1完成的,但我希望它也適用於其他版本。
演示設置:
% git init
% mkdir -p a/b/c/d e/f/g h/i j
% zargs -i.. -- **/*(/) -- touch ../.keep ../do-not-keep
% tree -aI .git .
.
|-- .gitignore
|-- a
| |-- .keep
| |-- b
| | |-- .keep
| | |-- c
| | | |-- .keep
| | | |-- d
| | | | |-- .keep
| | | | `-- do-not-keep
| | | `-- do-not-keep
| | `-- do-not-keep
| `-- do-not-keep
|-- e
| |-- .keep
| |-- do-not-keep
| `-- f
| |-- .keep
| |-- do-not-keep
| `-- g
| |-- .keep
| `-- do-not-keep
|-- h
| |-- .keep
| |-- do-not-keep
| `-- i
| |-- .keep
| `-- do-not-keep
`-- j
|-- .keep
`-- do-not-keep
10 directories, 21 files
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.