1、git diff 命令说明

commit操作之前,我们通常要确定一下自己在什么地方更改了代码,看看有没有误操作代码,这个时候git status命令的显示就比较简单了,仅仅是列出了修改过的文件,如果要查看具体修改了什么地方,就可以使用git diff命令。

比较有用的选项:--stat:显示有多少行发生变化,简洁的展示差异。

2、比较工作区与暂存区中文件的差别

查看工作区与暂存区内容的区别,使用无选项的git diff命令。

git diff file_name:获取指定文件的修改。

(1)首先在工作目录中创建一个hello.html文件,并添加到暂存区。

1
2
3
4
5
6
7
8
cd ~ && rm -rf ~/gitDiffTest

git init gitDiffTest && cd gitDiffTest

echo "hello git diff" >> hello.html

git add hello.html

(2)向hello.html文件添加一行新的内容,之后查看工作区与暂存区hello.html文件的区别。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
echo "hello git diff again" >> hello.html

git diff hello.html

diff --git a/hello.html b/hello.html

index 2435b37..bf8e610 100644

--- a/hello.html

+++ b/hello.html

@@ -1 +1,2 @@

 hello git diff

+hello git diff again

说明:

  • diff --git a/hello.html b/hello.html:表示进行比较的是hello.html文件的a版本(即变动前)和b版本(即变动后)。
  • index 2435b37..bf8e610 100644:表示两个版本的hash索引值,前边表示暂存区文件的索引,后边代表工作区中文件的索引。
  • 100644:表示文件模式,100代表普通文件,644代表文件具有的权限(同Linux文件权限)。
  • --- a/hello.html+++ b/hello.html:表示进行比较的两个文件,---表示变动前的版本,+++表示变动后的版本。
  • @@ -1 +1,2 @@:表示代码变动的位置,用两个@作为起首和结束。
    +1,2说明:分成三个部分:
    +表示变动后文件,1表示第一行,2表示连续2行。(也就是从第一行开始,有连续两行的内容。我个人的理解就是表示文件有几行内容。)
  • 最后一部分为文件变动的具体内容,每一行最前面的标志位:
    -代表第一个文件删除的行,用红色表示。
    +表示第二个文件新增的行,用绿色表示。
    无标志表示该行无变动。

这里在简单说明一下--stat选项的作用,如下:

1
2
3
git diff --stat hello.html
hello.html | 1 +
1 file changed, 1 insertion(+)

(3)将修改后的hello.html文件添加到暂存区中,再次来查看该文件。

1
2
git add .
git diff hello.html

没有任何输出,这就说明此时,工作区中hello.html文件的内容,与暂存区中hello.html文件的内容没有区别。

3、比较暂存区与本地库中文件的差别

查看暂存区与本地库中文件内容的区别,使用带--cached选项的git diff命令。

使用命令:git diff --cached file_name

提交到版本库之前

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
diff --git a/hello.html b/hello.html

new file mode 100644

index 0000000..bf8e610

--- /dev/null

+++ b/hello.html

@@ -0,0 +1,2 @@

+hello git diff

+hello git diff again

说明本地版本库当前为空

(1)接上面练习,把hello.html文件提交到本地版本库中。

1
2
git commit -m 'add hello.html file'
git diff --cached hello.html

此时为空,表示本地版本库已经与暂存区一致

(2)修改hello.html文件,然后添加到暂存区。

1
2
echo "hello again more" >> hello.html
git add .

(3)比较暂存区和本地版本库中hello.html文件的区别。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git diff --cached hello.html

diff --git a/hello.html b/hello.html

index bf8e610..f3d807d 100644

--- a/hello.html

+++ b/hello.html

@@ -1,2 +1,3 @@

 hello git diff

 hello git diff again

+hello again more

从上面文件中可以看出,暂存区中的hello.html文件比本地版本库中的hello.html文件,多出一行hello again more内容。(解读方式同上。)

4、总结git diff命令常见用法

  1. 比较工作区与暂存区:
    git diff命令,不加参数即默认比较工作区与暂存区。
  2. 比较暂存区与最新本地版本库(本地库中最近一次commit的内容):
    git diff --cached命令或者git diff --staged命令(1.6.1版本以上)。
  3. 比较工作区与最新本地版本库:
    git diff HEAD命令,如果HEAD指向的是master分支,那么HEAD还可以换成master
  4. 比较工作区与指定commit提交的差异:
    git diff commit-id命令。
  5. 比较暂存区与指定commit提交的差异:
    git diff --cached commit-id 命令。
  6. 比较两个commit提交之间的差异:
    git diff [<commit-id>] [<commit-id>]命令。
  7. 使用git diff命令打补丁,这个用法以后会详解,知道有这么回事就行。

提示:以上就不详细说明了,看前面举例两个例子,其他同理。

5、总结

以现在学到的知识点,git diff命令能解决我们两个问题:

  • 查看当前做的哪些更新还没有暂存?
    需要查看细节的时候,使用git diff命令。
  • 查看有哪些更新已经暂存起来,准备好了下次提交?
    需要查看细节的时候,使用git diff --cached命令或者git diff --staged命令。

6、参考

https://www.cnblogs.com/liuyuelinfighting/p/16227889.html