2015-11-07 4 views
2

Я играл с добавлением и фиксацией и использовал diff, чтобы показать несоответствия между ГОЛОВКОЙ, индексом и рабочим режком. Используя diff, я понимаю, что он должен указать, какие файлы были изменены, но также предполагается указать различия между файлами (например, если я пишу «привет» в файл, он должен сказать +hello (или что-то подобное) когда я запускаю diff, и он сравнивает эти два).Git diff не содержит конкретных изменений в файлах?

Однако, когда я использую diff, это только показывает мне, что файл был изменен; он не может показать, каковы эти изменения. Почему он не показывает отдельные изменения в файлах - фактический текст, который я добавил, что конкретно я удалил и т. Д.? Если diff этого не делает, есть ли команда, которую я могу использовать для нее?

спасибо.


Чтобы запустить полную проверку, я удалил .git и побежал git-init, потом нашел diff не показали различия для всех трех вариаций diff (git diff, git diff --cached и git 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

Если файл слишком мал, мерзавец не знает, как интерпретировать его (тест или двоичный): см «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 поставил проблему полностью! –

Смежные вопросы