简体   繁体   English

.gitignore 不适用于子目录中的文件

[英].gitignore not working for file in subdirectory

This is the content of my .gitignore file, which is at the root of my repository:这是我的 .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

The .DS_Store file in my project root directory is correctly ignored, but git status gives:我的项目根目录中的.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)

So why is ios/.DS_Store not ignored?那么为什么ios/.DS_Store没有被忽略呢?

Working on OS X 10.9.2, using the git version that Apple supplies: "git version 1.8.5.2 (Apple Git-48)"在 OS X 10.9.2 上工作,使用 Apple 提供的 git 版本:“git version 1.8.5.2 (Apple Git-48)”

Well, I think it is happening because of the extra space in the ignore rule, due to the comment you've added just afterwards and because of them the pattern mismatches.好吧,我认为这是因为忽略规则中的额外空间,由于您之后添加的注释以及模式不匹配。 Verified on git version 1.8.3.2 , Ubuntu 13.10git version 1.8.3.2Ubuntu 13.10

The following entry in .gitignore works all right (without any extra space) .gitignore的以下条目正常工作(没有任何额外空间)

.DS_Store

While neither of these work (Have space after the rule)虽然这些都不起作用(规则后有空间)

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

My guess is, your PROJECT_DIR/.DS_Store is already checked into the repo, hence doesn't show up in git status , are you sure your PROJECT_DIR/.DS_Store is not already a part of the repo?我的猜测是,您的PROJECT_DIR/.DS_Store已经签入回购,因此没有出现在git status ,您确定您的PROJECT_DIR/.DS_Store还不是回购的一部分吗?

Do a git log .DS_Store and see if any commits are thrown up.执行git log .DS_Store并查看是否有任何提交被抛出。

If yes, use the first ignore rule that works, and remove the exisitng .DS_Store like如果是,请使用第一个有效的忽略规则,并删除 exisitng .DS_Store类的

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

EDIT编辑

You are entering the comments in the wrong format, they have to be put at the beginning of the file/ignore rules.您输入的注释格式错误,必须将它们放在文件/忽略规则的开头。 Check this .检查这个

I verified your other ignore rule with comment - *.swp , and it doesn't work either.我用注释验证了你的另一个忽略规则 - *.swp ,它也不起作用。

From git-scm page on gitignoregitignore 上的 git-scm 页面

Each line in a gitignore file specifies a pattern. gitignore 文件中的每一行都指定了一个模式。 When deciding whether to ignore a path, Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome)在决定是否忽略某个路径时,Git 通常会检查来自多个来源的 gitignore 模式,按照以下优先级顺序,从高到低(在一个优先级内,最后匹配的模式决定结果)

When we believe .gitignore does NOT work, one reason could be those files are actually commited.当我们认为 .gitignore 不起作用时,原因之一可能是这些文件实际上已提交。 So the .gitignore cannot ignore it!所以 .gitignore 不能忽略它!

from this link https://www.atlassian.com/git/tutorials/saving-changes/gitignore the "git check-ignore -v pesty.file.name" can help to debug .gitignore.从这个链接https://www.atlassian.com/git/tutorials/saving-changes/gitignore “git check-ignore -v pety.file.name”可以帮助调试 .gitignore。 This is a very useful way to debug.这是一种非常有用的调试方法。 There are probably more other cases causing .gitignore NOT working.可能还有更多其他情况导致 .gitignore 不起作用。 If everyone could share their failed case, then how to make it work, the knowledge base can keep growing.如果每个人都可以分享他们失败的案例,那么如何使其发挥作用,知识库可以不断增长。 this "check-ignore -v" is our friend.这个“check-ignore -v”是我们的朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM