.gitignore 对空格敏感吗?

发布于 2024-12-20 10:43:51 字数 1027 浏览 5 评论 0原文

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

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

发布评论

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

评论(2

中二柚 2024-12-27 10:43:51

这在 git 2.0 中发生了变化。来自发行说明:

.gitignore 文件中的尾随空格,除非它们被引用
对于fnmatch(3),例如"path\ ",会被警告并被忽略。严格地
总的来说,这是一个向后不兼容的更改,但可能性很小
对于任何理智的用户来说,调整应该是明显且容易的。

This changed in git 2.0. From the release notes:

Trailing whitespaces in .gitignore files, unless they are quoted
for fnmatch(3), e.g. "path\ ", are warned and ignored. Strictly
speaking, this is a backward-incompatible change, but very unlikely
to bite any sane user and adjusting should be obvious and easy.

睡美人的小仙女 2024-12-27 10:43:51

.gitignore 对空格敏感。如果包含尾随空格,git 将无法识别您的文件。

在这一行中,有一个尾随空格:

pon2@kann4:~/foo$ echo "*.txt " > .gitignore

一旦我们解决了这个问题:

pon2@kann4:~/foo$ echo "*.txt" > .gitignore

问题就解决了:

pon2@kann4:~/foo$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
nothing added to commit but untracked files present (use "git add" to track)
pon2@kann4:~/foo$ 

.gitignore is whitespace sensitive. If you include trailing whitespace, git won't recognize your files.

In this line there's a trailing space:

pon2@kann4:~/foo$ echo "*.txt " > .gitignore

Once we fix that:

pon2@kann4:~/foo$ echo "*.txt" > .gitignore

The issue resolves:

pon2@kann4:~/foo$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
nothing added to commit but untracked files present (use "git add" to track)
pon2@kann4:~/foo$ 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文