[英]gitignore: Ignore everything from subfolder except files with specific extensions
[英]Gitignore everything except a subfolder (with every content)?
我想忽略所有內容,除了特定的子文件夾(及其所有內容!)。 我嘗試了可能的重復問題的解決方案,但沒有成功。
我需要一些簡單的東西,比如:
*
!That/Very/Folder/*
但這不起作用😞
*
!*/
!That/Very/Folder/**
!Also/This/Another/Folder/**
忽略所有內容,允許子文件夾 (!) ,然后允許特定文件夾內容(其中包含無限子文件夾)。
歸功於@Jepessen使其工作的中間部分。
您的.gitignore
幾乎可以工作,但原因並不簡單:第一條規則 ( *
) 告訴 Git 忽略存儲庫根目錄中的每個文件和目錄。 Git的榮譽,並忽略一切,包括That
目錄及其內容。 遵循的“unignore”規則不匹配That
子目錄中的任何內容,因為That
目錄與其內容一起被忽略,並且它們不起作用。
為了告訴 Git 不要忽略深層嵌套子目錄中的文件和目錄,您必須編寫 ignore 和 unignore 規則,讓它首先到達封閉的子目錄,然后添加您想要的規則。
您的.gitignore
文件應如下所示:
### Ignore everything ###
*
# But do not ignore "That" because we need something from its internals...
!That/
# ... but ignore (almost all) the content of "That"...
That/*
# ... however, do not ignore "That/Very" because we need to dig more into it
!That/Very/
# ... but we don't care about most of the content of "That/Very"
That/Very/*
# ... except for "That/Very/Folder" we care
!That/Very/Folder/
# ... and its content
!That/Very/Folder/*
我想忽略一切
將文件夾添加到 gitignore
除了特定的子文件夾(及其所有內容!)。
強制將文件夾添加到存儲庫
git add -f folder
編輯:
例如,當我需要保留日志文件夾而不是其內容時,我會使用此解決方案。 通常,當我認為永遠不會添加文件夾的內容時。 通常我只添加帶有-f
選項的path/to/folder/.gitkeep
文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.