簡體   English   中英

除文件外,以遞歸方式保留目錄和所有子目錄

[英]Keep directory and all subdirectories recursively except files

我的項目中有一個uploads文件夾,我需要git來跟蹤。
文件夾本身和所有子文件夾(我的意思是

上傳,
上傳/用戶,
上傳/用戶/型材,
上傳/用戶/型材/圖片

和其他路徑一樣)應該被跟蹤。

我在所有文件夾中添加了一個名為.gitkeep的空文件,我的.gitignore如下:

uploads/*
!.gitkeep

但是git沒有跟蹤子文件夾。

================================================== =============

CodeWizard建議通過命令創建.gitkeep文件,沒有解決辦法強制git忽略目錄中的所有文件和除了.gitkeep文件之外的子文件。 我使用此命令git-add -f在uploads目錄下創建的所有.gitkeep文件。

find . -name '.gitkeep' | xargs git add -f

.gitkeep添加到您需要的所有文件夾中。
Otherwize git不會跟蹤文件夾


腳本以遞歸方式創建.gitkeep

# find all the directories (-type d)
# filter the empty ones (-empty)
# ignore the .git folder
# add .gitkeep to all the matched folders

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 

如果你想要你也可以將它添加到最內層的文件夾 ,git將跟蹤所有父文件夾,當它添加到索引時

最后一行(sub1 / sub2 ...)是上述腳本的輸出

在此輸入圖像描述

雖然CodeWizard的解決方案允許您跟蹤文件夾,但它無法幫助您忽略其文件內容。

為此,您需要修改.gitignore以排除每個子文件夾被忽略。 例如:

uploads/**
!uploads/users
!uploads/users/profiles
!uploads/users/profiles/pics

您仍然需要每個子文件夾中的.gitkeep文件,或至少在最里面的子文件夾中。 CodeWizard建議的命令做得很好:

find . -type d -empty -not -path "./.git/*" -exec touch {}/.gitkeep \; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM