假设我有如下所示的dir结构:
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不过,这不管用。没有添加任何子目录。
发布于 2014-02-28 15:53:01
多亏@RyPeck的建议,我才开始编写bash脚本。最终,它作为一个简单的git钩子得到了最好的利用。
运行git commit,以下脚本将在提交消息出现之前执行。这允许我(A)确保从git缓存中删除这些上传目录中的文件(“uploads”)和(B)在每个目录中添加/触摸一个.gitkeep文件以维护目录结构。
.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发布于 2014-11-18 09:04:47
我使用的结构,看起来是这样的:
app/
.gitignore
uploads/
images/
.gitkeep
videos/
.gitkeep
docs/
.gitkeep我的应用/..gitignore文件的内容:
uploads/** # Ignore everything in 'uploads' folder
!uploads/**/ # exclude subdirectories
!.gitkeep # and .gitkeep files.发布于 2014-02-27 23:03:06
不要排除.gitkeep,只需将其添加到您想要保存的目录中的存储库。
您必须使用.gitkeep标志将-f文件添加到存储库中,以强制它覆盖该文件的.gitignore。
git add -f uploads/.gitkeephttps://stackoverflow.com/questions/22082534
复制相似问题