首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在目录结构中保存目录结构中的gitignore文件而维护目录结构?

问如何在目录结构中保存目录结构中的gitignore文件而维护目录结构?
EN

Stack Overflow用户
提问于 2014-02-27 23:01:05
回答 4查看 5.6K关注 0票数 6

假设我有如下所示的dir结构:

代码语言:javascript
复制
app/
  uploads/
    .gitkeep
    images/
      .gitkeep
    videos/
      .gitkeep
    docs/
      .gitkeep

我希望保留dir结构,但不包括其中的文件(显然.gitkeep除外)。医生说:

斜杠后面跟着两个连续的星号,然后斜杠匹配零或多个目录。例如,"a/**/b“与"a/b”、"a/x/b“、"a/x/y/b”等匹配。

所以,我希望这能起作用:

代码语言:javascript
复制
/app/uploads/**
!/app/uploads/**/.gitkeep

不过,这不管用。没有添加任何子目录。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-02-28 15:53:01

多亏@RyPeck的建议,我才开始编写bash脚本。最终,它作为一个简单的git钩子得到了最好的利用。

运行git commit,以下脚本将在提交消息出现之前执行。这允许我(A)确保从git缓存中删除这些上传目录中的文件(“uploads”)和(B)在每个目录中添加/触摸一个.gitkeep文件以维护目录结构。

.git/hooks/pre-commit

代码语言:javascript
复制
#!/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
票数 1
EN

Stack Overflow用户

发布于 2014-11-18 09:04:47

我使用的结构,看起来是这样的:

代码语言:javascript
复制
app/
  .gitignore
  uploads/
    images/
      .gitkeep
    videos/
      .gitkeep
    docs/
      .gitkeep

我的应用/..gitignore文件的内容:

代码语言:javascript
复制
uploads/**    # Ignore everything in 'uploads' folder
!uploads/**/  # exclude subdirectories
!.gitkeep     # and .gitkeep files.
票数 12
EN

Stack Overflow用户

发布于 2014-02-27 23:03:06

不要排除.gitkeep,只需将其添加到您想要保存的目录中的存储库。

您必须使用.gitkeep标志将-f文件添加到存储库中,以强制它覆盖该文件的.gitignore。

代码语言:javascript
复制
git add -f uploads/.gitkeep
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22082534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档