git查看修改内容-git基础(六)

对于已经修改的文件,你可能想查看具体修改了哪些内容。可以用 git diff 命令。
git diff 是比较同一个文件不同状态之间做了哪些修改。可以理解为同一个文件的两个副本之间做了哪些修改。
那是哪两个副本之间比较的呢?

一、查看已暂存和未暂存的修改

1. 查看未暂存的修改。
此时比较的是 已暂存(staged)和 已追踪未暂存(modified) 之间的修改部分。
此时执行 git status 查看文件状态:

$ git status
On branch master
No commits yet

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

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        hello.txt

提示 hello.txt未追踪。再执行git diff命令:

$ git diff

输出为空,表示当前已暂存文件和已修改文件没有区别。因为当前就没有modified状态的文件啊。
此时我们追踪 hello.txt ,执行命令 git add hello.txt ,并查看状态:

$ git add hello.txt

$ git status
On branch master

No commits yet

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

$ git diff

此时hello.txt已追踪,我们修改 hello.txt 的内容,第一行输入hello,保存。并执行 git diff。【注意此时 hello.txt 中已经有一行内容了,内容为hello world,我们将其改为了hello】

$ git status
On branch master
No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitignore
        new file:   hello.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:   hello.txt


$ git diff
diff --git a/hello.txt b/hello.txt
index 95d09f2..b6fc4c6 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1 @@
-hello world
 No newline at end of file
+hello
 No newline at end of file

a/hello.txt是已暂存文件,b/hello.txt 是工作区的文件
显示,已暂存文件内容是 hello world,工作区文件内容是hello。这里修改了。

执行 git add hello.txt 将修改暂存。再次查看状态和文件内容。

$ git add hello.txt

$ git status
On branch master
No commits yet

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

此时 hello.txt的文件内容为hello。

2. 查看已暂存修改
git diff --cached 命令
此时比较的是 提交至仓库的版本 和 暂存区文件 之间的修改部分。
到现在为止,我们还没有提交过版本。执行 git diff --cache

$ git diff --cached
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..e69de29
diff --git a/hello.txt b/hello.txt
new file mode 100644
index 0000000..b6fc4c6

此时比较内容为空,因为仓库里没有版本,我们还未提交过。现在我们提交项目的第一个版本。
提交版本的命令是 git commit -m <message> message是此次提交的备注,是必填项。
执行 git commit -m 初次提交 命令

$ git commit -m 初次提交
[master (root-commit) 337070e] 初次提交
 2 files changed, 1 insertion(+)
 create mode 100644 .gitignore
 create mode 100644 hello.txt

此时仓库的有了版本,hello.txt 的内容为 hello。此时我们修改内容为 hi,保存,并暂存文件,再执行 git diff --cached 查看。

$ git add hello.txt

$ git diff --cached
diff --git a/hello.txt b/hello.txt
index b6fc4c6..32f95c0 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1 @@
-hello
 No newline at end of file
+hi
 No newline at end of file

此时显示我们将 hello.txt 的内容由 hello 改为了 hi。

注意:--staged 和 --cached 是同义词
git diff --cached 等于 git diff --staged

总结:
掌握git diff命令的重点在于,明白Git文件的四种状态,以及文件的所在区域。
文件所在的区域有三个:工作区、暂存区、仓库。 
文件的流转方向是由工作区创建,add进入暂存区,commit进入仓库。
git diff的比较是从右向左。
git diff 比较暂存区与工作区文件的区别。
git diff --cached 比较仓库与暂存区文件的区别。



git文件所在区域

转载自:知优码  https://www.javaidea.cn/topic/1237.html

原文地址:https://www.cnblogs.com/beenupper/p/12526914.html