【问题标题】:.gitignore not working for file in subdirectory.gitignore 不适用于子目录中的文件
【发布时间】:2014-05-25 21:07:21
【问题描述】:

这是我的 .gitignore 文件的内容,它位于我的存储库的根目录中:

# grails stuff to ignore
target
*.iml
*.ipr
.idea
*.log
*.swp   # vi swap files
.DS_Store  # OS X Finder cache files

# cocoapods stuff to ignore
Pods
Podfile.lock

我的项目根目录中的 .DS_Store 文件被正确忽略,但 git status 给出:

$ git status
On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    ios/.DS_Store

nothing added to commit but untracked files present (use "git add" to track)

那么为什么ios/.DS_Store 不被忽略?

在 OS X 10.9.2 上工作,使用 Apple 提供的 git 版本:“git version 1.8.5.2 (Apple Git-48)”

【问题讨论】:

    标签: macos git gitignore


    【解决方案1】:

    嗯,我认为这是因为忽略规则中的额外空间,由于您之后添加的评论以及因为它们而导致模式不匹配。已在git version 1.8.3.2Ubuntu 13.10 验证

    .gitignore 中的以下条目可以正常工作(没有任何多余的空格)

    .DS_Store
    

    虽然这些都不起作用(规则后有空格)

    *.DS_Store # OS X Finder cache files
    .DS_Store # OS X Finder cache files
    

    我的猜测是,您的PROJECT_DIR/.DS_Store 已经签入到回购中,因此不会出现在git status 中,您确定您的PROJECT_DIR/.DS_Store 还不是回购的一部分吗?

    执行git log .DS_Store 并查看是否有任何提交被抛出。

    如果是,请使用第一个有效的忽略规则,并删除现有的.DS_Store like

    git rm --cached .DS_Store
    git commit -m "msg"
    

    编辑

    您以错误的格式输入 cmets,它们必须放在文件/忽略规则的开头。 Check this.

    我用评论验证了你的其他忽略规则 - *.swp,它也不起作用。

    来自git-scm page on gitignore

    gitignore 文件中的每一行都指定一个模式。在决定是否忽略路径时,Git 通常会检查来自多个来源的 gitignore 模式,优先级顺序如下,从高到低(在一个优先级内,最后匹配的模式决定结果)

    【讨论】:

    • 是的!!!!你是对的,你不能在文件名/模式之后添加 cmets。让我难过一阵子的男孩。
    • @SilverNightaFall 这对我也有用。给定目录foo/,我做了git rm --cached -r foo/-r 是递归的,因为它是一个目录)。然后我用git add foo/,一切都很好。
    【解决方案2】:

    当我们认为 .gitignore 不起作用时,一个原因可能是这些文件实际上已提交。所以.gitignore 不能忽略它!

    从此链接https://www.atlassian.com/git/tutorials/saving-changes/gitignore“git check-ignore -vpesty.file.name”可以帮助调试.gitignore。这是一种非常有用的调试方法。可能还有更多其他情况导致 .gitignore 不起作用。如果每个人都可以分享他们失败的案例,那么如何让它发挥作用,知识库可以不断增长。这个“check-ignore -v”是我们的朋友。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-23
      • 2016-04-05
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多