在git提交環節,存在三大部分:working tree, index file, commit
這三大部分中:
working tree:就是你所工作在的目錄,每當你在代碼中進行了修改,workingtree的狀態就改變了。
index file:是索引文件,它是連接workingtree和commit的橋樑,每當我們使用git-add命令來登記後,index file的內容就改變了,此時indexfile就和working tree同步了。
commit:是最後的階段,只有commit了,我們的代碼才真正進入了git倉庫。我們使用git-commit就是將indexfile裏的內容提交到commit中。
總結一下:
git diff:是查看working tree與index file的差別的。
git diff --cached:是查看index file與commit的差別的。
git diff HEAD:是查看workingtree和commit的差別的。(你一定沒有忘記,HEAD代表的是最近的一次commit的信息)
爲了更加清晰的闡釋這個關係,來給出一個實例。
[yaya@yaya-desktop]$ cat main.c
#include<stdio.h>
int main(int argc,char *argv[])
{
printf(“hello.\n”);
printf(“he was a student.\n”);
return 0;
}
然後git init, git add . , git commit;
之後你將源代碼修改爲:
[yaya@yaya-desktop]$ cat main.c
#include<stdio.h>
int main(int argc,char *argv[])
{
printf(“hello.\n”);
printf(“he was a student.\n”);
printf(“he was born in finland.\n”);
return 0;
}
此時你git add .,但不用執行git commit命令。然後你再將源代碼改爲:
- [yaya@yaya-desktop]$ cat main.c
- #include<stdio.h>
- int main(int argc,char *argv[])
- {
- printf(“hello.\n”);
- printf(“he was a student.\n”);
- printf(“he was born in finland.\n”);
- printf(“he is very clever!\n”);
- return 0;
- }
這個時候,你執行如下三個命令,仔細查看,我相信你會發現它們三個的區別的!
$ git diff
$ git diff –cached
$ git diff HEAD
講到這裏,基本上對git diff命令有了比較深入的瞭解了,現在你再使用git status看看輸出結果,樣子大概是這樣:
[yaya@yaya-desktop]$ git status
# On branch master
# Changes to be committed:
#
#
# modified: main.c
#
# Changed but not updated:
# (use “git add<file>…” to update what will becommitted)
#
# modified: main.c
#很明顯可以知道:
Changes to be committed表示已經存在於indexfile裏,但尚未提交。
Changed but not updated表示在workingtree已經做修改,但還沒有使用git add登記到index file裏。
好了,對於git diff的用法就簡單溫習到這裏吧。