【问题标题】:Git diff not listing specific changes to files?Git diff 没有列出对文件的特定更改?
【发布时间】:2015-11-07 13:03:54
【问题描述】:

我一直在尝试添加和提交,并使用diff 来显示 HEAD、索引和工作目录之间的差异。使用diff,我的理解是它应该指定哪些文件已更改,但也应该指定文件之间的差异(例如,如果我将“hello”写入文件,它应该说+hello(或类似的东西)当我运行diff并比较两者时)。

但是,当我使用diff 时,它只显示文件已更改;它没有显示这些变化是什么。为什么它不显示文件中的各个更改——我添加的实际文本、我删除的具体内容等?如果diff 不这样做,有没有我可以使用的命令?

谢谢。


为了运行完整的测试,我删除了.git 并运行了git-init,然后发现diff 没有显示diffgit diffgit diff --cached 和@987654334)的所有三种变体的差异@)。我使用的文件是t.txt。这是我的控制台,为了便于阅读,我对其进行了格式化; >> 是 Powershell 提示输入的地方,我在控制台中输入; cmets 是边写边写的,用>> # <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 <q@gmail.com>
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?

【问题讨论】:

标签: git


【解决方案1】:

如果文件太小,git 不知道如何解释它(测试或二进制):见“Why does Git treat this text file as a binary file?

您可以使用以下命令强制文本差异:

git diff --text

(或者你可以configure it作为文本)

【讨论】:

  • 谢谢,这就是问题所在。顺便说一句,感谢您包含有关配置 Git 以将其视为文本的链接——其中一个答案说不保存在 UTF-8 中会弄乱前 8000 个块或其他东西....无论如何,我检查并@ 987654324@ 保存为 Unicode。打开记事本 -> 另存为... -> UTF-8 使问题完全消失!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 2014-11-25
  • 1970-01-01
  • 2012-04-27
  • 2015-12-22
  • 1970-01-01
  • 2022-06-11
相关资源
最近更新 更多