我们假设我有一个类似的目录结构:
app/
uploads/
.gitkeep
images/
.gitkeep
videos/
.gitkeep
docs/
.gitkeep
我想保留dir结构但不包括文件(显然除了.gitkeep
)。文档说:
斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。例如,“a / ** / b”匹配“a / b”,“a / x / b”,“a / x / y / b”等等。
所以,我希望这可以做到这一点:
/app/uploads/**
!/app/uploads/**/.gitkeep
然而,这不起作用。没有添加任何子目录。
答案 0 :(得分:9)
我使用的结构如下所示:
app/
.gitignore
uploads/
images/
.gitkeep
videos/
.gitkeep
docs/
.gitkeep
我的app / .gitignore文件的内容:
uploads/** # Ignore everything in 'uploads' folder
!uploads/**/ # exclude subdirectories
!.gitkeep # and .gitkeep files.
答案 1 :(得分:3)
不要排除.gitkeep
,只需将其添加到要保留的目录中的存储库中。
您必须使用.gitkeep
标记将-f
文件添加到存储库,以强制它覆盖该文件的.gitignore
。
git add -f uploads/.gitkeep
答案 2 :(得分:1)
感谢@RyPeck的建议,我开始了bash脚本的路径。最终,它最终被用作简单的git hook。
运行git commit
,将在提交消息出现之前执行以下脚本。这允许我(A)确保从git缓存中删除这些上载目录中的文件(“un-add”),并且(B)在每个目录中添加/触摸.gitkeep文件以维护目录结构。 / p>
.git/hooks/pre-commit
#!/bin/sh
# pre-commit
################################################################################
# This is a site-specific hook to deal with locally-generated files that don't
# belong in the repo while maintaining the directory structure. The dir
# './images' is primarily managed via the CMS. This little ditty will
# remove cached files within the './images' directories (recursively)
# and adds a '.gitkeep' file to each folder (to maintain dir structure in repo).
################################################################################
keep=images
cd `pwd`
for cached in `find $keep -type d`
do
if [ -d $cached ]
then
touch $cached/.gitkeep
git rm -r --cached --quiet --ignore-unmatch $cached
git add -f $cached/.gitkeep # Force add with -f to deal with .gitignore conflicts
chmod 600 $cached/.gitkeep
fi
done
echo "Removed locally-generated files in '$keep'"
exit 0
答案 3 :(得分:-1)
或者,.gitignore
目录中的uploads
包含内容
* # ignore everything here below
!*/ # except don't ignore directories
!.git* # and don't ignore files beginning .git
然后像往常一样。