转自大神周沫凡:https://mofanpy.com/tutorials/others/git/
Git book:https://git-scm.com/book/zh/v2
目录
2.查看unstaged状态下的修改部分(git diff)
3.查看staged状态下的修改部分(git diff --cached)
4.同时查看staged & unstaged状态下的修改部分(git diff HEAD)
(commit记录了每一次的修改)在 git 中, 每一次提交(commit)的修改, 都会被单独的保存起来. 也可以说 git 的中的所有文件都是一次次修改累积起来的. 文件好比楼房, 每个 commit 记录 了盖楼需添加或者拿走的材料. 整个施工过程也被记录了下来.
1. log查看修改记录
如果我们对1.txt
文件进行一次修改, 添加这行代码:
a = 1
然后我们就能在 status
中看到修改还没被提交(changes not staged for commit)的信息了.
$ git status
# 输出
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: 1.py # 这里显示有一个修改还没被提交
no changes added to commit (use "git add" and/or "git commit -a")
我的实验代码:
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: 1.txt
no changes added to commit (use "git add" and/or "git commit -a")
(修改后未提交的解决:add+commit)所以我们先把这次修改添加 (add) 到可被提交 (commit) 的状态, 然后再提交 (commit) 这次的修改:
$ git add 1.py
$ git commit -m "change 1"
# 输出
[master fb51216] change 1
1 file changed, 1 insertion(+) # 提示文件有一处添加
我的实验代码:
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git add 1.txt
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git commit -m "change 1"
[master 2d350cb] change 1
1 file changed, 1 insertion(+)
(log:查看修改记录)再次查看 log
, 现在我们就能看到 create 1.py
和 change 1
这两条修改信息了. 而且做出这两条 commit
的 ID, 修改的 Author
, 修改 Date
也被显示在上面.
$ git log
# 输出
commit fb51216b081e00db3996e14edf8ff080fab1980a
Author: Morvan Zhou <mz@email.com>
Date: Tue Nov 29 00:24:50 2016 +1100
change 1
commit 13be9a7bf70c040544c6242a494206f240aac03c
Author: Morvan Zhou <mz@email.com>
Date: Tue Nov 29 00:06:47 2016 +1100
create 1.py
我的实验代码:
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git log
commit 2d350cbc1495ece3922acf9f3d797310215b90ab (HEAD -> master)
Author: jinger <yu@email.com>
Date: Thu May 23 14:30:44 2019 +0800
change 1
commit d19b6a53062abfd0c981753400c5bb70b6b57d9a
Author: jinger <yu@email.com>
Date: Thu May 23 14:21:08 2019 +0800
1.txt
如果删除一部分代码, 也会被记录上, 比如把 a = 1
改成 a = 2
, 再添加一个 b = 1
.
2.查看unstaged状态下的修改部分(git diff)
(unstaged状态下查看修改了什么内容:git diff)如果想要查看这次还没 add
(处于unstaged状态) 的修改部分 和上个已经 commit
的文件有何不同, 我们将使用 $ git diff
:
$ git diff
# 输出
diff --git a/1.py b/1.py
index 1337a53..ff7c36c 100644
--- a/1.py
+++ b/1.py
@@ -1 +1,2 @@
-a = 1 # 删除了 a = 1
+a = 2 # 添加了 a = 2
+b = 1 # 添加了 b = 1
我的实验代码:
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git diff
diff --git a/1.txt b/1.txt
index d25d49e..61ce15f 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
-a = 1
\ No newline at end of file
+a = 2
+b = 1
\ No newline at end of file
3.查看staged状态下的修改部分(git diff --cached)
(staged状态下查看修改了什么内容:git diff --cached)如果你已经 add
了这次修改, 文件变成了 “可提交状态” (staged), 我们可以在 diff
中添加参数 --cached
来查看修改:
$ git add . # add 全部修改文件
$ git diff --cached
# 输出
diff --git a/1.py b/1.py
index 1337a53..ff7c36c 100644
--- a/1.py
+++ b/1.py
@@ -1 +1,2 @@
-a = 1
+a = 2
+b = 1
我的实验代码:
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git add .
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git diff --cached
diff --git a/1.txt b/1.txt
index d25d49e..61ce15f 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
-a = 1
\ No newline at end of file
+a = 2
+b = 1
\ No newline at end of file
4.同时查看staged & unstaged状态下的修改部分(git diff HEAD)
还有种方法让我们可以查看 add
过 (staged) 和 没 add
(unstaged) 的修改, 比如我们再修改一下 1.py
但不 add
:
a = 2
b = 1
c = b
目前 a = 2
和 b = 1
已被 add
, c = b
是新的修改, 还没被 add
.
以下是diff 三种形式的不同结果
$ git diff HEAD
$ git diff
$ git diff --cached
如下:
# 对比三种不同 diff 形式
$ git diff HEAD # staged & unstaged
@@ -1 +1,3 @@
-a = 1 # 已 staged
+a = 2 # 已 staged
+b = 1 # 已 staged
+c = b # 还没 add 去 stage (unstaged)
-----------------------
$ git diff # unstaged
@@ -1,2 +1,3 @@
a = 2 # 注: 前面没有 +
b = 1 # 注: 前面没有 +
+c = b # 还没 add 去 stage (unstaged)
-----------------------
$ git diff --cached # staged
@@ -1 +1,2 @@
-a = 1 # 已 staged
+a = 2 # 已 staged
+b = 1 # 已 staged
我的实验代码:
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git diff HEAD
diff --git a/1.txt b/1.txt
index d25d49e..ac13cf6 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,3 @@
-a = 1
\ No newline at end of file
+a = 2
+b = 1
+c = b
\ No newline at end of file
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git diff
diff --git a/1.txt b/1.txt
index 61ce15f..ac13cf6 100644
--- a/1.txt
+++ b/1.txt
@@ -1,2 +1,3 @@
a = 2
-b = 1
\ No newline at end of file
+b = 1
+c = b
\ No newline at end of file
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git diff --cached
diff --git a/1.txt b/1.txt
index d25d49e..61ce15f 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
-a = 1
\ No newline at end of file
+a = 2
+b = 1
\ No newline at end of file
为了下节内容, 我们保持这次修改, 全部 add
变成 staged
状态, 并 commit
.
$ git add .
$ git commit -m "change 2"
# 输出
[master 6cc6579] change 2
1 file changed, 3 insertions(+), 1 deletion(-)
我的实验代码:
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git add .
Administrator@PC201710130840 MINGW64 ~/Desktop/daily-learning/gitTUT (master)
$ git commit -m "change 2"
[master 2f7e561] change 2
1 file changed, 3 insertions(+), 1 deletion(-)
总结:
- git log 查看修改记录
- git diff 查看unstaged(未add)状态下的修改部分
- git diff --cached 查看staged(已add)状态下的修改部分
- git diff HEAD 查看unstaged(已add + 未add)状态下的修改部分