git diff

本文介绍了Git的`git diff`命令,用于查看工作区与暂存区、HEAD、不同分支之间的文件差异,以及如何查看特定文件的差异。内容涵盖`git diff --cached`、`git diff HEAD`、`git diff <分支名1> <分支名2>`和`git diff --stat`等用法。
摘要由CSDN通过智能技术生成

git diff

git diff

显示工作区与暂存区之间的修改,即显示未被git add的修改
注意:git diff能够显示的修改是要被git追踪的文件的修改,那些新增文件,未git add前,未被git追踪,git diff是无法显示其修改的

DevelopdeMacBook-Pro-2:test developmacbook$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
 new file:   a.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
 modified:   a.txt

DevelopdeMacBook-Pro-2:test developmacbook$ git diff
diff --git a/a.txt b/a.txt
index 801c3fc..7f32e5a 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1,3 @@
 adfdf
 werw
+12343
DevelopdeMacBook-Pro-2:test developmacbook$ git diff a.txt
diff --git a/a.txt b/a.txt
index 801c3fc..7f32e5a 100644
--- a/a.txt
+++ b/a.txt
@@ -1,2 +1,3 @@
 adfdf
 werw
+12343
DevelopdeMacBook-Pro-2:test developmacbook$ git add a.txt
DevelopdeMacBook-Pro-2:test developmacbook$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
 new file:   a.txt

DevelopdeMacBook-Pro-2:test developmacbook$ git diff
DevelopdeMacBook-Pro-2:test developmacbook$ git diff

可以看到git diff <待比较文件路径1> <待比较文件路径2> ...

DevelopdeMacBook-Pro-2:test developmacbook$ git diff a.txt
...
DevelopdeMacBook-Pro-2:test developmacbook$ git diff a.txt b.txt
...
DevelopdeMacBook-Pro-2:test developmacbook$ git diff ./
...

当 文件 or 目录不存在时,会报错

DevelopdeMacBook-Pro-2:test developmacbook$ git diff c.txt
fatal: ambiguous argument 'c.txt': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

git diff --cached = git diff --staged

显示暂存区与最近一次commit(HEAD)之间的修改,即显示未被git add的修改,,即显示已git add但还未git commit的修改

DevelopdeMacBook-Pro-2:test developmacbook$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
 new file:   a.txt
 new file:   b.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
 modified:   a.txt
 modified:   b.txt

DevelopdeMacBook-Pro-2:test developmacbook$ git diff --cached
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..7f32e5a
--- /dev/null
+++ b/a.txt
@@ -0,0 +1,3 @@
+adfdf
+werw
+12343
diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..e69de29
DevelopdeMacBook-Pro-2:test developmacbook$ git diff --staged
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..7f32e5a
--- /dev/null
+++ b/a.txt
@@ -0,0 +1,3 @@
+adfdf
+werw
+12343
diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..e69de29

git diff HEAD

显示工作区与最近一次commit(HEAD)之间的修改,即显示所有未git commit(包括已add和未add)的修改

DevelopdeMacBook-Pro-2:test developmacbook$ git diff HEAD
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
DevelopdeMacBook-Pro-2:test developmacbook$ git log --oneline
fatal: your current branch 'master' does not have any commits yet

上面结果,是因为当前还没有一次commit的缘故

DevelopdeMacBook-Pro-2:test developmacbook$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
 new file:   a.txt
 new file:   b.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
 modified:   a.txt
 modified:   b.txt

从上面git status结果可以看到,工作区有2个文件修改待add,暂存区同样存在两个文件修改待commit

DevelopdeMacBook-Pro-2:test developmacbook$ git diff --cached
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..7f32e5a
--- /dev/null
+++ b/a.txt
@@ -0,0 +1,3 @@
+adfdf
+werw
+12343
diff --git a/b.txt b/b.txt
new file mode 100644
index 0000000..e69de29
DevelopdeMacBook-Pro-2:test developmacbook$ git diff
diff --git a/a.txt b/a.txt
index 7f32e5a..dad7df1 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,4 @@
 adfdf
 werw
 12343
+safadf
diff --git a/b.txt b/b.txt
index e69de29..6d1f869 100644
--- a/b.txt
+++ b/b.txt
@@ -0,0 +1 @@
+fadfadfa

可以看到

  1. 暂存区中的修改是:创建文件a.txt并新增了3行内容,创建文件b.txt
  2. 工作区中待add的修改是:a.txt文件新增了1行内容,b.txt文件新增了1行内容

那么,当执行git commit将暂存区的修改提交到版本库后,再执行git add -Agit diff HEAD,结果应该是:a.txt文件新增了1行内容,b.txt文件新增了1行内容

DevelopdeMacBook-Pro-2:test developmacbook$ git commit -m 'init'
[master (root-commit) fa2ec56] init
 2 files changed, 3 insertions(+)
 create mode 100644 a.txt
 create mode 100644 b.txt
DevelopdeMacBook-Pro-2:test developmacbook$ git log --oneline
fa2ec56 (HEAD -> master) init
DevelopdeMacBook-Pro-2:test developmacbook$ git add -A
DevelopdeMacBook-Pro-2:test developmacbook$ git diff HEAD
diff --git a/a.txt b/a.txt
index 7f32e5a..dad7df1 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,4 @@
 adfdf
 werw
 12343
+safadf
diff --git a/b.txt b/b.txt
index e69de29..6d1f869 100644
--- a/b.txt
+++ b/b.txt
@@ -0,0 +1 @@
+fadfadfa
DevelopdeMacBook-Pro-2:test developmacbook$ git diff

但是工作区和暂存区是一致的

git diff <分支名1> <分支名2>

= git diff <分支名1>…<分支名2>

比较两个分支上最后commit内容的区别。

比如,先git fetch下,然后比较远程的master与当前分支已经commit的内容差别:git diff origin/master feature/test,此时是以origin/master分支为基础进行比较的,若把feature/test分支写在前面,则是以feature/test分支为基础进行比较。

注意: 分支名也可以是某次提交的hashcode,比较两次提交的差异,其实可以这么理解:使用分支,则是取分支的最近一次提交

DevelopdeMacBook-Pro-2:test developmacbook$ git log --oneline
5b01bfd (HEAD -> master) second commit
fa2ec56 init
DevelopdeMacBook-Pro-2:test developmacbook$ git diff fa2ec56 5b01bfd
diff --git a/a.txt b/a.txt
index 7f32e5a..dad7df1 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,4 @@
 adfdf
 werw
 12343
+safadf
diff --git a/b.txt b/b.txt
index e69de29..6d1f869 100644
--- a/b.txt
+++ b/b.txt
@@ -0,0 +1 @@
+fadfadfa
DevelopdeMacBook-Pro-2:test developmacbook$ git diff fa2ec56 master
diff --git a/a.txt b/a.txt
index 7f32e5a..dad7df1 100644
--- a/a.txt
+++ b/a.txt
@@ -1,3 +1,4 @@
 adfdf
 werw
 12343
+safadf
diff --git a/b.txt b/b.txt
index e69de29..6d1f869 100644
--- a/b.txt
+++ b/b.txt
@@ -0,0 +1 @@
+fadfadfa
DevelopdeMacBook-Pro-2:test developmacbook$ git diff master 5b01bfd
DevelopdeMacBook-Pro-2:test developmacbook$ git diff master fa2ec56
diff --git a/a.txt b/a.txt
index dad7df1..7f32e5a 100644
--- a/a.txt
+++ b/a.txt
@@ -1,4 +1,3 @@
 adfdf
 werw
 12343
-safadf
diff --git a/b.txt b/b.txt
index 6d1f869..e69de29 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +0,0 @@
-fadfadfa

git diff --stat

--stat参数可以得到存在差异的文件列表

DevelopdeMacBook-Pro-2:test developmacbook$ git diff --cached --stat
 a.txt | 1 +
 b.txt | 1 +
 2 files changed, 2 insertions(+)
DevelopdeMacBook-Pro-2:test developmacbook$ git diff HEAD --stat
 a.txt | 1 +
 b.txt | 1 +
 2 files changed, 2 insertions(+)

可以看到,2个文件存在插入变更:a.txt,b.txt

具体某个文件的差异

DevelopdeMacBook-Pro-2:test developmacbook$ git diff master fa2ec56 a.txt
diff --git a/a.txt b/a.txt
index dad7df1..7f32e5a 100644
--- a/a.txt
+++ b/a.txt
@@ -1,4 +1,3 @@
 adfdf
 werw
 12343
-safadf
DevelopdeMacBook-Pro-2:test developmacbook$ git diff HEAD a.txt b.txt

可以看到,git diff后面接具体文件列表,便只显示所列文件的差异


参考:

Git笔记:git diff 的用法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值