git 忽略异常未按预期工作

发布于 2024-09-18 09:48:00 字数 1145 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小伙你站住 2024-09-25 09:48:00

您的 .gitignore 异常不起作用,因为“如果排除该文件的父目录,则无法重新包含该文件”(来源)。这是“Git 中与性能相关的怪癖”(来源)。

作为一种丑陋但实用的解决方法,请对文件路径中的每个目录执行以下操作:

  1. 排除目录的内容。
  2. 重新包含指向您的文件的子目录(最后,重新包含您的文件)。

您的条目将如下所示,每个两行部分对应于一个目录级别的上述两个步骤:

Demos/*
!Demos/path/

Demos/path/*
!Demos/path/to/

Demos/path/to/*
!Demos/path/to/file/

Demos/path/to/file/*
!Demos/path/to/file/file.cpp

注释:

第一行不是 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:

  1. Exclude the contents of the directory.
  2. Re-include the sub-directory leading to your file (resp. finally, re-include your file).

Your entries would look like this, with each two-line section corresponding to the two steps above for one directory level:

Demos/*
!Demos/path/

Demos/path/*
!Demos/path/to/

Demos/path/to/*
!Demos/path/to/file/

Demos/path/to/file/*
!Demos/path/to/file/file.cpp

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/*.

熟人话多 2024-09-25 09:48:00

请按如下方式添加到 .gitignore

folderUnderRepoRoot/**/*
!folderUnderRepoRoot/tracking.cpp

但请确保至少有一个文件从 folderUnderRepoRoot 文件夹添加到 git 索引。

Do add to .gitignore as follows:

folderUnderRepoRoot/**/*
!folderUnderRepoRoot/tracking.cpp

But make sure, that at least a one file added to git index from the folderUnderRepoRoot folder.

暗恋未遂 2024-09-25 09:48:00

如果只是一个文件,我通常不会修改 .gitignore

以下内容应该忽略 .gitignore 并允许您添加该文件

git add -f Demos/path/to/file/file.cpp

If it's just one file, I wouldn't normally modify .gitignore

The following should ignore .gitignore and allow you to add the file

git add -f Demos/path/to/file/file.cpp
清晰传感 2024-09-25 09:48:00

您可以尝试将其替换为 !/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?

浮云落日 2024-09-25 09:48:00

对我来说,情况有很大不同。我尝试忽略以“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文