2011-12-07 53 views
3

我的.gitignore文件似乎工作不可預測。這裏有一個例子:.gitignore是否對空白敏感?

我就與文件跳回到bar.txt,我想忽略一個新的回購富:

[email protected]:~$ mkdir foo 
[email protected]:~$ cd foo/ 
[email protected]:~/foo$ touch bar.txt 
[email protected]:~/foo$ git init 
Initialized empty Git repository in /home/pon2/foo/.git/ 
[email protected]:~/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顯示爲未跟蹤。所以我告訴混帳忽略.txts,但我不小心添加一些尾隨空白:

[email protected]:~/foo$ echo "*.txt " > .gitignore 

現在,當我檢查回購情況,Git並不跳回到bar.txt忽視:

[email protected]:~/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) 

這是怎麼回事上?

+0

只要您以問題的形式*提問您的問題,就可以問和回答您自己的問題:http://meta.stackexchange.com/questions/12513/should-i-not -answer-my-own-questions否則,這只是一篇博文,並不屬於此處。 –

+1

因此,在gitignore手冊頁中向Git開發者發送一個補丁。 – jamessan

+0

@GregHewgill,謝謝,修正。 – wvoq

回答

2

.gitignore是空白敏感的。如果您包含尾隨空格,git將無法識別您的文件。

在這一行有一個尾隨空格:

[email protected]:~/foo$ echo "*.txt " > .gitignore 

一旦我們解決這個問題:

[email protected]:~/foo$ echo "*.txt" > .gitignore 

問題解決:

[email protected]:~/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) 
[email protected]:~/foo$ 
+1

我創建了一個文件:「b.txt」,你的原點.gitignore正確地忽略了這個文件。如果你期待着不同的東西,你到底在期待什麼? –

+1

@CharlesBailey,帶有尾隨空白的文件名看起來像是一種病態的邊緣情況。我希望,如果我不小心把尾隨的空白放在我的.gitignore中,git會做正確的事情而忽略它。在你看來,這是否是正確的事情取決於你,但是我希望你至少能夠意識到這是非常混亂的行爲。我沒有看到這方面的任何文件,所以我把它作爲麪包屑放在了所有與我一樣困惑的人面前。 – wvoq

+0

我同意通常最好避免使用空白的文件名,但如果有人想明確忽略它們,應該允許。如果你想理智檢查你的.gitignore的變化,你應該考慮在提交之前使用'git diff --check'。 (或者'git log -p --check'查找可能不好的以前版本。) –

0

這改變了混帳2.0。從release notes:在的.gitignore文件

尾部空格,除非它們被引用爲 fnmatch(3),例如"path\ ",被警告和忽略。嚴格來說,這是一種向後不兼容的變化,但極不可能讓任何理智的用戶咬牙切齒,調整應該明顯而容易。

相關問題