git 忽略异常未按预期工作
WinXP + mysisGit1.7
在我的 .gitignore 文件中,但仍然看不到 git 跟踪的 Demos/path/to/file/file.cpp 。
我有以下条目:
Demos/
!Demos/path/to/file/file.cpp
绝对路径是: c:\Project\Demos\path\to\file\file.cpp
可能出了什么问题?请帮忙,谢谢。
编辑:
我发现 mysisGit .gitignore 在 WindowsXP 上的工作方式只能忽略某些类型的文件,然后排除某些相同类型的文件。例如:
*.bak
!tracking.bak
!/path/to/file/tracking2.bak
忽略文件夹并排除该文件夹下的某些文件不起作用。以下行不通:
/folderUnderRepoRoot/
!/folderUnderRepoRoot/tracking.cpp
也
anyFolderNamedLikeThis/
!anyFolderNamedLikeThis/tracking.cpp
!/anyFolderNamedLikeThis/tracking.cpp
行不过,我确实发现有一个例外。有一种解决方法可以排除被忽略文件夹(而不是其子文件夹)下的文件。 这是可行的。
/folderUnderRepoRoot/*
/folderUnderRepoRoot/tracking.cpp
但是这种方式仅当文件不在任何子文件夹中时受到限制,因此它不是很有用。
所以我最终仍然提交了大部分源文件,即使我在跟踪其他一些大项目时只对几个文件感兴趣。这意味着有一堆文件我不会碰,但仍然需要提交它们。
这是另一个线程也有类似的问题。
WinXP + mysisGit1.7
In my .gitignore file, but still can't see Demos/path/to/file/file.cpp
being tracked by git.
I have below entries:
Demos/
!Demos/path/to/file/file.cpp
The absolute path is: c:\Project\Demos\path\to\file\file.cpp
What could be wrong? Please help, thanks.
EDIT:
I found the way how mysisGit .gitignore work on WindowsXP can only ignore certain type of file, then exclude some files with same type. For example:
*.bak
!tracking.bak
!/path/to/file/tracking2.bak
It doesn't work ignore folder and exclude some files under that folder. Below won't work:
/folderUnderRepoRoot/
!/folderUnderRepoRoot/tracking.cpp
Nor
anyFolderNamedLikeThis/
!anyFolderNamedLikeThis/tracking.cpp
!/anyFolderNamedLikeThis/tracking.cpp
However, I do find that there's an exception. There's a work-around way to exclude files just right under the ignored folder (not to its subfolder). This works.
/folderUnderRepoRoot/*
/folderUnderRepoRoot/tracking.cpp
But this way is only limited when the file is not in any subfolder, so it's not so useful.
So I end up still commit most of source files, even I was only interested in a few files while tracking some others big project. Which means there're a bunch of files I won't touch but still need to commit them.
Here is another thread that had similar problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的
.gitignore
异常不起作用,因为“如果排除该文件的父目录,则无法重新包含该文件”(来源)。这是“Git 中与性能相关的怪癖”(来源)。作为一种丑陋但实用的解决方法,请对文件路径中的每个目录执行以下操作:
您的条目将如下所示,每个两行部分对应于一个目录级别的上述两个步骤:
注释:
第一行不是
Demos/
,与问题作者尝试过。Demos
是我们文件的父目录,因此我们必须在之后重新包含它——这意味着我们不必首先排除它。相反,我们首先仅排除其内容:Demos/*
。Your
.gitignore
exception does not work because "It is not possible to re-include a file if a parent directory of that file is excluded" (source). This is "a performance related quirk in Git" (source).As an ugly-but-functional workaround, do this for every directory in the path to your file:
Your entries would look like this, with each two-line section corresponding to the two steps above for one directory level:
Notes:
The first line is not
Demos/
, unlike what the question author tried.Demos
is a parent directory of our file, so we would have to re-include it right afterwards – means, we do not have to exclude it in the first place. Instead, we start by excluding only its contents:Demos/*
.请按如下方式添加到
.gitignore
:但请确保至少有一个文件从
folderUnderRepoRoot
文件夹添加到 git 索引。Do add to
.gitignore
as follows:But make sure, that at least a one file added to git index from the
folderUnderRepoRoot
folder.如果只是一个文件,我通常不会修改
.gitignore
以下内容应该忽略
.gitignore
并允许您添加该文件If it's just one file, I wouldn't normally modify
.gitignore
The following should ignore
.gitignore
and allow you to add the file您可以尝试将其替换为
!/Demos/path/to/file/file.cpp
(注意感叹号后面的前导斜杠),然后重试吗?Can you try replacing it with
!/Demos/path/to/file/file.cpp
(note the leading slash after the exclamation mark) instead and try again?对我来说,情况有很大不同。我尝试忽略以“secrets”开头的所有文件,除了此文件“secrets.[stage].json”,因为它只是开发人员的模板。
这不起作用
Secrets*
!secrets.[stage].json
我发现这只是因为括号是需要转义的特殊字符所以这就是解决方案
Secrets*
!secrets.[stage].json
相同其他特殊字符也会出现问题。
由于 .gitignore 使用通配模式来匹配文件名。
您可以在此处找到通配模式的文档
https://linux.die.net/man/7/glob
这对我来说比官方 .gitignore 文档好得多。
for me, the case was so much different. I tried to ignore all files starting with "secrets" except this file "secrets.[stage].json" as it is just a template for developers.
This doesn't work
secrets*
!secrets.[stage].json
I found out that it was just because bracket is a special character that need to be escaped so this was the solution
secrets*
!secrets.[stage].json
The same problem happens with other special characters.
As .gitignore uses globbing patterns to match against file names.
You can find the documentation of globbing patterns here
https://linux.die.net/man/7/glob
Which was much better for me than the official .gitignore documentation.