.gitignore is whitespace sensitive?

别来无恙 提交于 2019-12-07 07:05:28

问题


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?


回答1:


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.




回答2:


.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$ 


来源:https://stackoverflow.com/questions/8422045/gitignore-is-whitespace-sensitive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!