git log -p与git show vs. git diff

时间:2014-09-01 15:13:55

标签: git git-diff git-log git-show

命令git log -pgit showgit diff如何相关,为什么会使用另一个?

给出以下4次提交的回购:

  

提交 - 最后提交
  commitc
  commitb
  coomita - 初始提交

以下git命令有什么区别?:

git log -p commitb commitd  
git show commitb commitd  
git diff commitb commitd  

git log -p commitd commitb  
git show commitd commitb  
git diff commitd commitb  

git log -p commitb..commitd  
git show commitb..commitd  
git diff commitb..commitd

git log -p commitd..commitb  
git show commitd..commitb  
git diff commitd..commitb

git log -p commitb...commitd  
git show commitb...commitd  
git diff commitb...commitd

git log -p commitd...commitb  
git show commitd...commitb  
git diff commitd...commitb

1 个答案:

答案 0 :(得分:26)

git log A B显示了提交AB的历史记录(基本上生成了每个提交和历史记录的联合集)。通常你想要git log A..B,它也可以写成git log ^B A(显示所有可以从A到达,但不是^)来自B)。此范围也可以为空(例如B..A为空,因为从B可以访问的每个提交也可以从A到达。因此,当使用"错误"时,您也不会从git log获得任何输出。参数。

git show是一个非常通用的命令,根据其参数产生不同的输出。您可以传递一个或多个提交,它将显示提交信息(作者身份,时间戳,提交消息,来自先前提交的差异)。将解析诸如a..d的提交范围,并且每个提交都将单独显示。您也可以传递路径,git show会显示文件的内容。您还可以使用语法commit:path/to/file指定特定版本的文件。如果传递目录,git show将显示上次提交更改该目录的提交信息。

git diff通常需要比较两棵树或两个文件。 (它也可以不带参数,而是比较索引/暂存区域)。提交会自动解包到其对应的树中(树对象描述存储库的某个状态)。对于diff案例,语法A..B以静默方式解析为A B,并显示从commit / tree AB

所需的更改

范围A...B表示"每次提交都可以从A或B到达,但不能同时从两个&#34 ;.

  • git log一起使用时,它会向您显示这些提交,并且在与分支分支一起使用时大多数情况下才有意义,即"显示两者之间的每个提交不同,但隐藏这些提交两个分支都有共同点#34;。
  • 对于git diff,此语法是git diff $(git merge-base A B) B的语法糖,即BA的历史分歧以来的变化。
  • 使用git show,您只需获取该范围内每个提交的提交信息。

如果还有什么不清楚,请在评论中告诉我。


这是您在原始问题中发布的存储库的输出:

$ git init
 Initialized empty Git repository in ...
$  cd SO
$ ls
$ echo a > a
$ git add a
$ git commit
[master (root-commit) 7b66fe5] initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 a
$ git tag a
$ echo b >> a
$ echo b > b
$ git add a b
$ git commit
[master ee884fe] commit b
 2 files changed, 2 insertions(+)
 create mode 100644 b
$ git tag b
$ echo c >> a
$ echo c >> b
$ echo c > c
$ git add a b c
$ git commit
[master 8abaaff] commit c
 3 files changed, 3 insertions(+)
 create mode 100644 c
$ git tag c
$ echo d >> a
$ echo d > b
$ git add a b
$ git commit
[master 08adc85] commit d
 2 files changed, 2 insertions(+), 2 deletions(-)
$ git tag d
$ git log -p b d --
commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d

commit 8abaaff681be0bcaa16c946feb2989959348c9f4
Author: ...
Date:   Mon Sep 1 17:21:40 2014 +0200

    commit c

diff --git a/a b/a
index 422c2b7..de98044 100644
--- a/a
+++ b/a
@@ -1,2 +1,3 @@
 a
 b
+c
diff --git a/b b/b
index 6178079..9ddeb5c 100644
--- a/b
+++ b/b
@@ -1 +1,2 @@
 b
+c
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c

commit ee884fea5d0266280845348175ac0af5083a9bbf
Author: ...
Date:   Mon Sep 1 17:21:05 2014 +0200

    commit b

diff --git a/a b/a
index 7898192..422c2b7 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
 a
+b
diff --git a/b b/b
new file mode 100644
index 0000000..6178079
--- /dev/null
+++ b/b
@@ -0,0 +1 @@
+b

commit 7b66fe5999039c53ffbe5a5ffe07c13a5c213455
Author: ...
Date:   Mon Sep 1 17:20:39 2014 +0200

    initial commit

diff --git a/a b/a
new file mode 100644
index 0000000..7898192
--- /dev/null
+++ b/a
@@ -0,0 +1 @@
+a
$ git show b d --
commit ee884fea5d0266280845348175ac0af5083a9bbf
Author: ...
Date:   Mon Sep 1 17:21:05 2014 +0200

    commit b

diff --git a/a b/a
index 7898192..422c2b7 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
 a
+b
diff --git a/b b/b
new file mode 100644
index 0000000..6178079
--- /dev/null
+++ b/b
@@ -0,0 +1 @@
+b

commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d
$ git diff b d --
diff --git a/a b/a
index 422c2b7..d68dd40 100644
--- a/a
+++ b/a
@@ -1,2 +1,4 @@
 a
 b
+c
+d
diff --git a/b b/b
index 6178079..4bcfe98 100644
--- a/b
+++ b/b
@@ -1 +1 @@
-b
+d
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c
$ git log -p d b --
commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d

commit 8abaaff681be0bcaa16c946feb2989959348c9f4
Author: ...
Date:   Mon Sep 1 17:21:40 2014 +0200

    commit c

diff --git a/a b/a
index 422c2b7..de98044 100644
--- a/a
+++ b/a
@@ -1,2 +1,3 @@
 a
 b
+c
diff --git a/b b/b
index 6178079..9ddeb5c 100644
--- a/b
+++ b/b
@@ -1 +1,2 @@
 b
+c
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c

commit ee884fea5d0266280845348175ac0af5083a9bbf
Author: ...
Date:   Mon Sep 1 17:21:05 2014 +0200

    commit b

diff --git a/a b/a
index 7898192..422c2b7 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
 a
+b
diff --git a/b b/b
new file mode 100644
index 0000000..6178079
--- /dev/null
+++ b/b
@@ -0,0 +1 @@
+b

commit 7b66fe5999039c53ffbe5a5ffe07c13a5c213455
Author: ...
Date:   Mon Sep 1 17:20:39 2014 +0200

    initial commit

diff --git a/a b/a
new file mode 100644
index 0000000..7898192
--- /dev/null
+++ b/a
@@ -0,0 +1 @@
+a
$ git show d b --
commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d

commit ee884fea5d0266280845348175ac0af5083a9bbf
Author: ...
Date:   Mon Sep 1 17:21:05 2014 +0200

    commit b

diff --git a/a b/a
index 7898192..422c2b7 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
 a
+b
diff --git a/b b/b
new file mode 100644
index 0000000..6178079
--- /dev/null
+++ b/b
@@ -0,0 +1 @@
+b
$ git diff d b --
diff --git a/a b/a
index d68dd40..422c2b7 100644
--- a/a
+++ b/a
@@ -1,4 +1,2 @@
 a
 b
-c
-d
diff --git a/b b/b
index 4bcfe98..6178079 100644
--- a/b
+++ b/b
@@ -1 +1 @@
-d
+b
diff --git a/c b/c
deleted file mode 100644
index f2ad6c7..0000000
--- a/c
+++ /dev/null
@@ -1 +0,0 @@
-c
$ git log -p b..d --
commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d

commit 8abaaff681be0bcaa16c946feb2989959348c9f4
Author: ...
Date:   Mon Sep 1 17:21:40 2014 +0200

    commit c

diff --git a/a b/a
index 422c2b7..de98044 100644
--- a/a
+++ b/a
@@ -1,2 +1,3 @@
 a
 b
+c
diff --git a/b b/b
index 6178079..9ddeb5c 100644
--- a/b
+++ b/b
@@ -1 +1,2 @@
 b
+c
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c
$ git show b..d --
commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d

commit 8abaaff681be0bcaa16c946feb2989959348c9f4
Author: ...
Date:   Mon Sep 1 17:21:40 2014 +0200

    commit c

diff --git a/a b/a
index 422c2b7..de98044 100644
--- a/a
+++ b/a
@@ -1,2 +1,3 @@
 a
 b
+c
diff --git a/b b/b
index 6178079..9ddeb5c 100644
--- a/b
+++ b/b
@@ -1 +1,2 @@
 b
+c
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c
$ git diff b..d --
diff --git a/a b/a
index 422c2b7..d68dd40 100644
--- a/a
+++ b/a
@@ -1,2 +1,4 @@
 a
 b
+c
+d
diff --git a/b b/b
index 6178079..4bcfe98 100644
--- a/b
+++ b/b
@@ -1 +1 @@
-b
+d
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c
$ git log -p d..b --
$ git show d..b --
$ git diff d..b --
diff --git a/a b/a
index d68dd40..422c2b7 100644
--- a/a
+++ b/a
@@ -1,4 +1,2 @@
 a
 b
-c
-d
diff --git a/b b/b
index 4bcfe98..6178079 100644
--- a/b
+++ b/b
@@ -1 +1 @@
-d
+b
diff --git a/c b/c
deleted file mode 100644
index f2ad6c7..0000000
--- a/c
+++ /dev/null
@@ -1 +0,0 @@
-c
$ git log -p b...d --
commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d

commit 8abaaff681be0bcaa16c946feb2989959348c9f4
Author: ...
Date:   Mon Sep 1 17:21:40 2014 +0200

    commit c

diff --git a/a b/a
index 422c2b7..de98044 100644
--- a/a
+++ b/a
@@ -1,2 +1,3 @@
 a
 b
+c
diff --git a/b b/b
index 6178079..9ddeb5c 100644
--- a/b
+++ b/b
@@ -1 +1,2 @@
 b
+c
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c
$ git show b...d --
commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d

commit 8abaaff681be0bcaa16c946feb2989959348c9f4
Author: ...
Date:   Mon Sep 1 17:21:40 2014 +0200

    commit c

diff --git a/a b/a
index 422c2b7..de98044 100644
--- a/a
+++ b/a
@@ -1,2 +1,3 @@
 a
 b
+c
diff --git a/b b/b
index 6178079..9ddeb5c 100644
--- a/b
+++ b/b
@@ -1 +1,2 @@
 b
+c
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c
$ git diff b...d --
diff --git a/a b/a
index 422c2b7..d68dd40 100644
--- a/a
+++ b/a
@@ -1,2 +1,4 @@
 a
 b
+c
+d
diff --git a/b b/b
index 6178079..4bcfe98 100644
--- a/b
+++ b/b
@@ -1 +1 @@
-b
+d
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c
$ git log -p d...b --
commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d

commit 8abaaff681be0bcaa16c946feb2989959348c9f4
Author: ...
Date:   Mon Sep 1 17:21:40 2014 +0200

    commit c

diff --git a/a b/a
index 422c2b7..de98044 100644
--- a/a
+++ b/a
@@ -1,2 +1,3 @@
 a
 b
+c
diff --git a/b b/b
index 6178079..9ddeb5c 100644
--- a/b
+++ b/b
@@ -1 +1,2 @@
 b
+c
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c
$ git show d...b --
commit 08adc8512e63588e6f01533b2a0f762342521b05
Author: ...
Date:   Mon Sep 1 17:22:06 2014 +0200

    commit d

diff --git a/a b/a
index de98044..d68dd40 100644
--- a/a
+++ b/a
@@ -1,3 +1,4 @@
 a
 b
 c
+d
diff --git a/b b/b
index 9ddeb5c..4bcfe98 100644
--- a/b
+++ b/b
@@ -1,2 +1 @@
-b
-c
+d

commit 8abaaff681be0bcaa16c946feb2989959348c9f4
Author: ...
Date:   Mon Sep 1 17:21:40 2014 +0200

    commit c

diff --git a/a b/a
index 422c2b7..de98044 100644
--- a/a
+++ b/a
@@ -1,2 +1,3 @@
 a
 b
+c
diff --git a/b b/b
index 6178079..9ddeb5c 100644
--- a/b
+++ b/b
@@ -1 +1,2 @@
 b
+c
diff --git a/c b/c
new file mode 100644
index 0000000..f2ad6c7
--- /dev/null
+++ b/c
@@ -0,0 +1 @@
+c
$ git diff d...b --
$