1. 程式人生 > >git diff 命令

git diff 命令

工作區 工作 高版本 dir directory 版本 you pos body

1.git diff 用於查看未暫存的文件更新了哪些部分,也就是以修改未暫存的文件 和 已暫存的相比

1.創建文件diff.txt
echo blue01>diff.txt

2.git add把diff.txt添加到暫存區中
修改文件diff.txt內容為blue02,這時可以知道工作區中diff.txt內容為blue02,暫存區中diff.txt內容為blue01

3.git diff 比較工作區與暫存區
$ git diff diff.txt
warning: LF will be replaced by CRLF in diff.txt.
The file will have its original line endings in your working directory.
diff --git a/diff.txt b/diff.txt
index 6be2f5a..8c140fc 100644
--- a/diff.txt
+++ b/diff.txt
@@ -1 +1 @@
-blue01   //暫存區
+blue02   //工作區
2.git diff --cached 用於暫存區和已經提交到分支上的進行比較


4.執行git diff --cached diff.txt 發現結果未空,也就是沒有改變這個為什麽呢?
原因是:這是工作區是blue02,暫存區是blue01,分支上也是blue01,因此暫存區與分支比較,沒有改變的內容,如果想出現修改內容,則把工作區的blue02提交到暫存區即可。

5.git add diff.txt  提交到暫存區

6.執行git diff --cached diff.txt
$ git diff --cached diff.txt
diff --git a/diff.txt b/diff.txt
index 6be2f5a..8c140fc 100644
--- a/diff.txt
+++ b/diff.txt
@@ -1 +1 @@
-blue01    //分支上
+blue02    //暫存區中

總結:git diff 工作區與暫存區對比,git diff --cached  diff.txt  暫存區和倉庫中分支做比較,(Git 1.6.1 及更高版本還允許使用 git diff --staged 代替 git diff --cached,效果是相同的,但更好記些。)

git diff 命令