git diff

Published: 13 May 2014 Category: git

参考文档:http://marklodato.github.io/visual-git-guide/index-zh-cn.html

接触过Linux的同学都知道有个diff命令,同样git也有个diff命令,下面就结合实例介绍一下。

1.新建一个目录git-test,初始化一下仓库,做一次简单的提交:

mkdir git-test
cd git-test/
git init
echo 0 > test
git add .
git commit -am "init"  

2.稍作修改,再做一次简单的提交:

linux-geek:/home/tmp/git-test # echo 1 >> test
linux-geek:/home/tmp/git-test # git commit -am "1"
[master 3e82b9e] 1
 1 files changed, 1 insertions(+), 0 deletions(-)  

3.演示git diff:

linux-geek:/home/tmp/git-test # echo 2 >> test
linux-geek:/home/tmp/git-test # git diff
diff --git a/test b/test
index 0d66ea1..4539bbf 100644
--- a/test
+++ b/test
@@ -1,2 +1,3 @@
 0
 1
+2

我想说的是git diff比较工作区和暂存区里面的内容。

4.演示git diff --cached或--staged:

linux-geek:/home/tmp/git-test # git add test
linux-geek:/home/tmp/git-test # git diff --cached
diff --git a/test b/test
index 0d66ea1..4539bbf 100644
--- a/test
+++ b/test
@@ -1,2 +1,3 @@
 0
 1
+2
linux-geek:/home/tmp/git-test #  

我想说的是git diff --cached比较暂存区和commit里面的内容。

5.演示git diff 某个commit:

linux-geek:/home/tmp/git-test # git diff HEAD~1
diff --git a/test b/test
index 573541a..4539bbf 100644
--- a/test
+++ b/test
@@ -1 +1,3 @@
 0
+1
+2  

我想说的是git diff 某个commit比较工作区和commit里面的内容。

6.演示git diff commit0 commit1:

linux-geek:/home/tmp/git-test # git diff HEAD~1 HEAD
diff --git a/test b/test
index 573541a..0d66ea1 100644
--- a/test
+++ b/test
@@ -1 +1,2 @@
 0
+1  

相信这个不用我多说了吧!

7.演示git diff commit0 commit1 --stat:

linux-geek:/home/tmp/git-test # git diff HEAD~1 HEAD --stat
 test |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)  

加上--stat,会给出两个commit修改了哪些文件。

最后,给出一张图:

git-diff