为什么git diff会根据提交修订产生不同的结果?

Pea*_*key 3 git

我是版本控制世界的新手,刚刚开始使用git.运行命令时,我有以下输出git log

commit 3b33318e20a64f4395bba416fe60f50f9e0002d1
Author: pm
Date:   Thu Jan 24 08:42:24 2013 +1300

    added fourth line to test1

commit 37f2ce409cdc971958add1fcf6585064f5c0d61d
Author: pm
Date:   Thu Jan 24 08:41:24 2013 +1300

    first commit
Run Code Online (Sandbox Code Playgroud)

据我所知,git log显示上一次提交后的最新提交.现在如果我运行git diff HEAD HEAD~我理解的命令"向我展示最新提交和上一次提交之间的区别",我得到以下输出:

diff --git a/test1 b/test1
index c6f02c2..e41a5e4 100644
--- a/test1
+++ b/test1
@@ -1,4 +1,3 @@
 This is a test document
 This is the second line in the document
 And the third
-Added a fourth line
Run Code Online (Sandbox Code Playgroud)

它显示了减号,当我修改文件test1时我添加了一个新行但是如果我运行git diff HEAD~ HEAD我理解为"显示第二次最后提交和最新提交之间的区别"的命令,它会显示以下内容输出:

diff --git a/test1 b/test1
index e41a5e4..c6f02c2 100644
--- a/test1
+++ b/test1
@@ -1,3 +1,4 @@
 This is a test document
 This is the second line in the document
 And the third
+Added a fourth line
Run Code Online (Sandbox Code Playgroud)

它显示我添加了带加号的第四行.

文件比较是否重要?我原以为你比较文件的方式是"比较最新的比较"git diff HEAD HEAD~

Phi*_*ßen 5

git diff A B 列出从A到B的所需更改.如果交换参数,则会得到相反的更改,如示例所示.

你可以说Git可以知道A发生在B之前,然后为git diff A B和产生相同的输出git diff B A.但是,恕我直言,这不是一个好主意,原因有两个:

  1. 显示从左到右的变化更加一致.它也可以派上用场,特别是如果你在它上面构建脚本.

  2. 有时不清楚哪个提交在另一个之前,例如:

      C
     / \
    A   B
    
    Run Code Online (Sandbox Code Playgroud)

    git diff A B:在A之前是B还是B之前?

  • 我不完全确定你的意思.你通常用`git log`查找refs,然后按照你感兴趣的方向进行比较.例如,如果你想看看最后一次提交改变了什么,那就是`git diff head~1 head`.因此它显示了从现在到现在之前的一次提交所需的更改:-) (2认同)