git diff --cached和git diff的区别

git init之后的状态

maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git init
Initialized empty Git repository in /home/maijunjin/mycode/myGit/testGit/.git/
maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

 

新建一个test文件,git status之后的状态是 Untracked

maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ vi test
maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ ls
test
maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    test
nothing added to commit but untracked files present (use "git add" to track)

 

 

git add test文件之后的状态是Changes to be committed

maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git add test
maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   test
#
maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ 

比较一下,在使用了git add test之后的git diff和git diff --cached的区别:

maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git diff
maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git diff --cached
diff --git a/test b/test
new file mode 100644
index 0000000..a5bce3f
--- /dev/null
+++ b/test
@@ -0,0 +1 @@
+test1

添加了test文件,这个test文件添加了test1内容

 

编辑test文件,添加了test2内容,没有提交

maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git diff
diff --git a/test b/test
index a5bce3f..bae42c5 100644
--- a/test
+++ b/test
@@ -1 +1,2 @@
 test1
+test2

git diff有反应了,显示添加了test2内容

 

git diff --cached的情况如下:

maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git diff --cached
diff --git a/test b/test
new file mode 100644
index 0000000..a5bce3f
--- /dev/null
+++ b/test
@@ -0,0 +1 @@
+test1

 

 

再次git add test

maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git add test
maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git diff --cached
diff --git a/test b/test
new file mode 100644
index 0000000..bae42c5
--- /dev/null
+++ b/test
@@ -0,0 +1,2 @@
+test1
+test2

可以看出git diff --cached比较的git add之后文件的情况,而git diff 是比较没有放到暂存区的文件的变化,所以,提交之后git diff的没有显示内容:

maijunjin@maijunjin-Inspiron-7420:~/mycode/myGit/testGit$ git diff

总结:git diff --cached比较git add和git commit之间的区别,而git diff是比较暂存区的文件和暂存区之间的区别

git diff --cached显示暂存区做了那些修改

git diff显示项目中的文件做了那些修改(都必须是git add过了才会现实)

 

 

posted on 2013-04-25 00:29  只愿软禁  阅读(695)  评论(0)    收藏  举报