2014-09-01 30 views
13

git log -p,git show和git diff如何关联以及为什么会使用另一个?git log -p vs git show vs git diff

鉴于回购有以下4个提交:

commitd - 最后提交
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 
+0

我打开了多个终端窗口,并运行所有的命令,但我不能得出有意义的任何概括。试图找出我自己几天后,没有与我的谷歌福运气我想我一定是错过了一些东西,所以我在这里发布的问题。 – 2014-09-01 15:26:26

+1

请参阅[gitrevisions文档](https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html)。如果你对图论有所了解,它应该会有很大帮助;如果不是,它可能相当不透明。此外,似乎kernel.org手册页(我已链接到)已过时... – torek 2014-09-01 18:20:34

+1

@torek感谢您的优秀参考。除了知道Git使用DAG之外,我并没有真正理解图论,但我希望更多的初学者资源开始于对细节的解释,因为我不觉得100%自信我知道我的日常工作与Git的互动,虽然我现在已经经历了很多次基础。 – 2014-09-03 14:27:28

回答

20

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通常期望两棵树或两个文件进行比较。 (它也可以不采用参数,而是比较索引/分段区域)。一个提交会自动将解包成到其相应的树中(树对象描述了存储库的某个状态)。语法A..B被静默决心A B的差速器箱,并会显示从提交/树AB

范围A...B意味着需要改变“每一个承诺到达从A或B,但不可两者兼得”。

  • git log使用它会告诉你这些提交与发散的分支使用时大多才有意义,即“显示每一个是两者之间的不同,但隐藏那些两个分支有共同提交”。
  • git diff这个语法是git diff $(git merge-base A B) B,即“自A发散的历史B变化语法糖。
  • 随着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 -- 
$ 
+0

好吧,除了'git log AB'不会执行'A..B':'git log'的参数与'git rev-list'的处理方式相同,就产生修改检查,这实际上是两套工会的结合。 (否则一切都正确,虽然有时候'A..B'和'B..A'都是非空的。) – torek 2014-09-01 18:14:42

+0

@torek:谢谢你的抬头。我已更正第一段。希望有没有错误:) – knittl 2014-09-01 18:40:07

+0

@ knittl非常感谢您的详细回复 – 2014-09-03 14:23:18