递归保存目录和所有子目录,但文件

时间:2017-03-05 12:05:54

标签: git gitignore

我的项目中有一个上传文件夹,我需要git来跟踪 文件夹本身和所有子文件夹(我的意思是

上传,
  上传/用户,
  上传/用户/型材,
  上传/用户/个人资料/图片

应该跟踪

和其他类似的路径。

我在所有文件夹中添加了一个名为.gitkeep的空文件,我的.gitignore如下:

uploads/*
!.gitkeep

但是git没有跟踪子文件夹。

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

CodeWizard建议通过命令创建.gitkeep文件,没有解决办法强制git忽略目录中的所有文件和除了.gitkeep文件之外的子文件。我使用这个命令来git-add -f在uploads目录下创建的所有.gitkeep文件。

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

2 个答案:

答案 0 :(得分:3)

.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 ...)是上述脚本的输出

enter image description here

答案 1 :(得分:1)

虽然CodeWizard的解决方案允许您跟踪文件夹,但它无法帮助您忽略其文件内容。

为此,您需要修改.gitignore以排除每个子文件夹被忽略。例如:

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

您仍然需要每个子文件夹中的.gitkeep个文件,或者至少在最里面的子文件夹中。 CodeWizard建议的命令非常出色:

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