2015-11-07 29 views
2

我一直在玩添加和提交,並使用diff來顯示HEAD,索引和工作目錄之間的差異。使用diff,我的理解是,它應該指定哪些文件已經更改,但它也應該指定文件之間的差異(例如,如果我將「hello」寫入文件,應該說+hello(或類似的東西)當我運行diff和它比較兩者)。Git diff不列出文件的特定更改?

但是,當我使用diff時,它只顯示文件已更改;它沒有顯示這些變化是什麼。爲什麼它不顯示文件中的個別更改 - 我添加的實際文本,我已刪除的具體文本等等?如果diff不這樣做,是否有一個我可以使用它的命令?

謝謝。


要運行一個完整的測試,我刪除.git就跑​​,然後發現diff沒有表現出差異爲diff所有三種變體(git diffgit diff --cachedgit diff HEAD)。我使用的文件是t.txt。這是我爲了可讀性而格式化的控制檯; >>是Powershell提示輸入的地方,我輸入到控制檯中;評論是我寫的,用>> # <comment>表示。這是我的控制檯輸出:

>> # Starting with nothing. 

>> git status 
fatal: Not a git repository (or any of the parent directories): .git 

>> git init 
Initialized empty Git repository in C:/Users/q/Documents/GitHub/.git/ 

>> ls 

>> git status 
On branch master 
Initial commit 
nothing to commit (create/copy files and use "git add" to track) 

>> echo "New file created." > t.txt 

>> git status 
On branch master 
Initial commit 
Untracked files: 
    (use "git add <file>..." to include in what will be committed) 
     t.txt 
nothing added to commit but untracked files present (use "git add" to track) 


>> git add . 

>> git status 
On branch master 
Initial commit 
Changes to be committed: 
    (use "git rm --cached <file>..." to unstage) 
     new file: t.txt 


>> #open t.txt and add a second line of text to it 

>> git status 
On branch master 
Initial commit 
Changes to be committed: 
    (use "git rm --cached <file>..." to unstage) 
     new file: t.txt 
Changes not staged for commit: 
    (use "git add <file>..." to update what will be committed) 
    (use "git checkout -- <file>..." to discard changes in working directory) 
     modified: t.txt 


>> git diff 
diff --git a/t.txt b/t.txt 
index e7a4f8a..fe91a49 100644 
Binary files a/t.txt and b/t.txt differ 

>> #How can I see the exact change? 
>> #Something like: ''' 
>> # 
>> # + This is the added line. 
>> # 
>> # ''' in the diff? 


>> # I can take this even further by making the first commit, modifying, then staging the file 
           # with `git add`, then editing again, and then run all three `git-diff`s 
           # (git diff, git diff --cached, git diff HEAD) and none of them will specify any actual 
           # changes in the files; Git still just lists the files that have discrepancies, without 
           # listing any of the detials about /what/ is actually different. 


>> git add . 

>> git commit -m "Committed with the second line." 
[master (root-commit) 14acc45] Committed with the second line. 
1 file changed, 0 insertions(+), 0 deletions(-) 
create mode 100644 t.txt 

>> git diff 

>> git log 
commit 14acc455b16ba26cdea1661166b0ffc3fa089784 
Author: q <[email protected]> 
Date: Sat Nov 7 04:29:20 2015 -0800 
    Committed with the second line. 

>> git diff HEAD 

>> git diff HEAD^ 
fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree. 
Use '--' to separate paths from revisions, like this: 
'git <command> [<revision>...] -- [<file>...]' 


>> #We change the file again to add a third line 


>> git status 
On branch master 
Changes not staged for commit: 
    (use "git add <file>..." to update what will be committed) 
    (use "git checkout -- <file>..." to discard changes in working directory) 
     modified: t.txt 
no changes added to commit (use "git add" and/or "git commit -a") 


>> git diff 
diff --git a/t.txt b/t.txt 
index fe91a49..006c33a 100644 
Binary files a/t.txt and b/t.txt differ 


>> git diff HEAD 
diff --git a/t.txt b/t.txt 
index fe91a49..006c33a 100644 
Binary files a/t.txt and b/t.txt differ 


>> git add . 

>> #Modify file again. 

>> git status 
On branch master 
Changes to be committed: 
    (use "git reset HEAD <file>..." to unstage) 
     modified: t.txt 
Changes not staged for commit: 
    (use "git add <file>..." to update what will be committed) 
    (use "git checkout -- <file>..." to discard changes in working directory) 
     modified: t.txt 


>> git diff 
diff --git a/t.txt b/t.txt 
index 006c33a..57a6754 100644 
Binary files a/t.txt and b/t.txt differ 

>> git diff HEAD 
diff --git a/t.txt b/t.txt 
index fe91a49..57a6754 100644 
Binary files a/t.txt and b/t.txt differ 

>> git diff --cached 
diff --git a/t.txt b/t.txt 
index fe91a49..006c33a 100644 
Binary files a/t.txt and b/t.txt differ 

>>## But how do they differ? 
+0

參見[這個答案](http://stackoverflow.com/a/15009104/1517864),治療二進制文件爲文本 – jayant

回答

2

如果文件過小,Git並不知道如何解釋它(測試或二進制):詳見「Why does Git treat this text file as a binary file?

可以強制文字與差異:

git diff --text 

(或者你可以configure it文本)

+0

謝謝,那是問題所在。順便說一下,謝謝你包含關於配置Git將其視爲文本的鏈接 - 其中一個答案表示,不保存在UTF-8中會弄亂前8000個塊或什麼......無論如何,我檢查了「 echo「HI」> t.txt'保存爲Unicode。打開記事本 - >另存爲... - > UTF-8使問題完全消失! –

相關問題