Git 学习札记(三)关于 git diff 操作及其参数

Git 学习札记(三)

关于Git中diff操作的比较

git diff ,git diff –cached,git diff –staged,git diff HEAD – filename

先做一个实验:

chia@edogawachia-sphinx:~/mygit$ cat readme.txt 
This is a readme file
created by E.Chia 2018/02/06
modified by E.Chia 1st time
chia@edogawachia-sphinx:~/mygit$ git status 
位于分支 master
无文件要提交,干净的工作区
chia@edogawachia-sphinx:~/mygit$ sudo vim readme.txt 
chia@edogawachia-sphinx:~/mygit$ cat readme.txt 
This is a readme file
created by E.Chia 2018/02/06
modified by E.Chia 1st time
add this to show staged version
chia@edogawachia-sphinx:~/mygit$ sudo git add 'readme.txt'
chia@edogawachia-sphinx:~/mygit$ git status
位于分支 master
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

    修改:     readme.txt

chia@edogawachia-sphinx:~/mygit$ sudo vim readme.txt 
chia@edogawachia-sphinx:~/mygit$ cat readme.txt 
This is a readme file
created by E.Chia 2018/02/06
modified by E.Chia 1st time
add this to show staged version
add this to show working directory version

这样三个区域的版本都不一样了,branch中的最短,stage暂存区的有一句add this to show staged version,工作区的多一句 add this to show working directory version。

下面测试各种 diff 的用法:

chia@edogawachia-sphinx:~/mygit$ git diff
diff --git a/readme.txt b/readme.txt
index 3e42036..e24b3e0 100644
--- a/readme.txt
+++ b/readme.txt
@@ -2,3 +2,4 @@ This is a readme file
 created by E.Chia 2018/02/06
 modified by E.Chia 1st time
 add this to show staged version
+add this to show working directory version
chia@edogawachia-sphinx:~/mygit$ git diff --cached
diff --git a/readme.txt b/readme.txt
index d995350..3e42036 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,4 @@
 This is a readme file
 created by E.Chia 2018/02/06
 modified by E.Chia 1st time
+add this to show staged version
chia@edogawachia-sphinx:~/mygit$ git diff --staged
diff --git a/readme.txt b/readme.txt
index d995350..3e42036 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,4 @@
 This is a readme file
 created by E.Chia 2018/02/06
 modified by E.Chia 1st time
+add this to show staged version
chia@edogawachia-sphinx:~/mygit$ git diff HEAD
diff --git a/readme.txt b/readme.txt
index d995350..e24b3e0 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,5 @@
 This is a readme file
 created by E.Chia 2018/02/06
 modified by E.Chia 1st time
+add this to show staged version
+add this to show working directory version
chia@edogawachia-sphinx:~/mygit$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index d995350..e24b3e0 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,3 +1,5 @@
 This is a readme file
 created by E.Chia 2018/02/06
 modified by E.Chia 1st time
+add this to show staged version
+add this to show working directory version

可以看到,git diff 比较工作区和暂存区,git diff –cache (或者–staged)比较版本和暂存区,git diff HEAD – filename 比较branch上的HEAD指向的版本与工作区的区别(这里的HEAD可以是任意指针,即用哈希值表示的 commit id ,或者HEAD~等)。这样三者中的任意两者都可以比较了。

2018-02-07 16:25:46

你可能感兴趣的:(Git,札记)