.gitignore 不忽略子目录中先前提交的文件,尽管 *.<fileending>

.gitignore not ignoring previously commited file in subdirectory despite *.<fileending>

忘记将其包含在我的 .gitignore 文件中后,文件 'Labb1.mpf' 被意外提交并发送到上游。之后我将行 '*.mpf' ​​添加到我的 gitignore 文件中,但是当我处于 运行 git 状态时该文件仍然显示在 'Untracked Files' 下并且当我使用 'git add .'。即使在上游删除文件的提交之后,它仍然在未跟踪的文件中。 Labb.mpf 位于具有以下结构的子目录中:

C:.
│   .gitignore
|
└───Labb1
    │   Labb1.mpf

我最终设法通过在 git 忽略文件中专门写出完整路径名来使其不包含在 'git add .' 中。

Labb1/Labb1.mpf

如果我注释掉这一行并且只注释掉 运行 '*.mpf',文件将再次使用 git 添加命令暂存。

这在某种程度上是一种解决方案,但我不知道为什么,对于在其他情况下很可能会出现的问题,这不是一个非常优雅的解决方案。有没有人知道如何使用星号命令成功忽略(在本例中为 .mpf 文件)?

我试过一个和两个星号。

(更新)解决方案

感谢 VonC 的提示,我了解到 .gitignore 声明后的任何空格都会使语句失败。我有这样写的'.mpf'语句

*.mpf #Remove project definition file

如您所见,语句和注释之间有一个空格。通过将注释移到单独的一行(并删除空格),语句按预期执行。

首先检查,当它工作时,你的文件确实被你的规则忽略了:

git check-ignore -v -- Labb1/Labb1.mpf

然后,double-check你的 eol (end of line) 风格 .gitignore:确保只使用 LF,而不是 CRLF。
并确保您的 *.mpf 没有任何尾随空格。

OP Simon Jansson确认最后一点:

Any whitespace after a .gitignore declaration makes the statement fail.
I had the '.mpf' statement written like this

*.mpf #Remove project definition file

where, as you can see, there is a whitespace between the statement and the comment.
By moving the comment to a separate line (and removing the whitespace), the statement did as intended.