.gitignore 对空格敏感吗?
我的 .gitignore 文件似乎无法预测地工作。这是一个示例:
我使用文件 bar.txt 创建了一个新的存储库 foo,我想忽略该文件:
pon2@kann4:~$ mkdir foo
pon2@kann4:~$ cd foo/
pon2@kann4:~/foo$ touch bar.txt
pon2@kann4:~/foo$ git init
Initialized empty Git repository in /home/pon2/foo/.git/
pon2@kann4:~/foo$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bar.txt
nothing added to commit but untracked files present (use "git add" to track)
正如预期的那样,bar.txt 显示为未跟踪。所以我告诉 git 忽略 .txts,但我不小心添加了一些尾随空格:
pon2@kann4:~/foo$ echo "*.txt " > .gitignore
现在,当我检查存储库状态时,git 不会忽略 bar.txt:
pon2@kann4:~/foo$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# bar.txt
nothing added to commit but untracked files present (use "git add" to track)
这是怎么回事?
My .gitignore file seems to be working unpredictably. Here is an example:
I make a new repo foo with a file bar.txt, which I want to ignore:
pon2@kann4:~$ mkdir foo
pon2@kann4:~$ cd foo/
pon2@kann4:~/foo$ touch bar.txt
pon2@kann4:~/foo$ git init
Initialized empty Git repository in /home/pon2/foo/.git/
pon2@kann4:~/foo$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# bar.txt
nothing added to commit but untracked files present (use "git add" to track)
As expected, bar.txt shows up as untracked. So I tell git to ignore .txts, but I accidentally add some trailing whitespace:
pon2@kann4:~/foo$ echo "*.txt " > .gitignore
Now when I check the repo status, git doesn't ignore bar.txt:
pon2@kann4:~/foo$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
# bar.txt
nothing added to commit but untracked files present (use "git add" to track)
What's going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这在 git 2.0 中发生了变化。来自发行说明:
This changed in git 2.0. From the release notes:
.gitignore 对空格敏感。如果包含尾随空格,git 将无法识别您的文件。
在这一行中,有一个尾随空格:
一旦我们解决了这个问题:
问题就解决了:
.gitignore is whitespace sensitive. If you include trailing whitespace, git won't recognize your files.
In this line there's a trailing space:
Once we fix that:
The issue resolves: