文章目录
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
可以看到
- 暂存区中的修改是:创建文件a.txt并新增了3行内容,创建文件b.txt
- 工作区中待
add
的修改是:a.txt文件新增了1行内容,b.txt文件新增了1行内容
那么,当执行git commit
将暂存区的修改提交到版本库后,再执行git add -A
,git 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
后面接具体文件列表,便只显示所列文件的差异
参考: