1. 程式人生 > >git的檔案狀態以及git diff結果分析 (staged已暫存的,unstaged取消暫存的)

git的檔案狀態以及git diff結果分析 (staged已暫存的,unstaged取消暫存的)


git file

git庫所在的資料夾(即.git所在的資料夾)中的檔案大抵就是這四種狀態。

untracked:未跟蹤,此檔案在資料夾中,但並沒有加入git庫,不參與版本控制。 通過”git add”,”git commit”可將它置入跟蹤庫。

unmodify:檔案已經庫中,未修改,即版本庫中的檔案快照內容與資料夾中完全一致。這種型別的檔案有兩個去處,如果它被修改,而成為modified。如果使用”git rm”移出版本庫,則成為untracked檔案。

modified:檔案已修改,僅僅是修改,並沒有進行其它操作。這個檔案也有兩個去處,通過”git add”可進入暫存(staged)狀態,使用”git checkout”則丟棄修改,返因到unmodify狀態。這個checkout很好理解,就是取出庫中檔案,覆蓋當前檔案吧。

staged:暫存狀態。執得”git commit”則將修改同步到庫中,這時庫中的檔案與本地檔案又一致了,於是檔案是unmodify狀態。執行”git reset HEAD filenam”取消暫存,檔案狀態變為modified。


git diff

可以用來比較:

1.staging area和working area的檔案 (無其他引數時)

[plain] view plaincopyprint?
  1. git diff  

2.master分支和working area的檔案 (用master引數)

[plain] view plaincopyprint?
  1. git diff master   
3.HEAD指向的內容和working area的檔案 [plain] view plaincopyprint?
  1. git diff HEAD  

4.用遠端master分支比較當前工作區

[plain] view plaincopyprint?
  1. git diff refs/remotes/origin/master  

5.經常還要用到master分支的某個檔案的歷史版本和working area的該檔案的比較

[plain] view plaincopyprint?
  1. git diff 0c5ee16a6a4c849d0ae0448caa8ff174399c7c3c ./socket_helper.cpp  
上面的命令中, diff後面的引數指的是commit id, ./socket_helper.cpp是要比較的檔案路徑。


diff的命令輸出格式注意:

[plain] view plaincopyprint?
  1. ---代表原始檔  
  2. +++代表目標檔案  
通常working area的檔案都是被當作目標檔案來看待。

-開頭的行,是隻出現在原始檔中的行

+開頭的行,是隻出現在目標檔案中的行

空格開頭的行,是原始檔和目標檔案中都出現的行

差異按照差異小結進行組織,每個差異小結的第一行都是定位語句,由@@開頭,@@結尾。

[plain] view plaincopyprint?
  1. chenshu@chenshu-yangzhou-home:~/kaimei/data_service/src$ git diff 0c5ee16a6a4c849d0ae0448caa8ff174399c7c3c ./socket_helper.cpp  
  2. diff --git a/data_service/src/socket_helper.cpp b/data_service/src/socket_helper.cpp  
  3. index d606452..047e213 100755  
  4. --- a/data_service/src/socket_helper.cpp  
  5. +++ b/data_service/src/socket_helper.cpp  
  6. @@ -4,6 +4,7 @@  
  7.  #include "data/login_response.h"  
  8.  #include "data/heartbeat_response.h"  
  9.  #include "helper/parser.h"  
  10. +#include "helper/time_measure.h"  
  11.  #include <booster/log.h>  
  12.  #include "exception/socket_error.h"  
  13.  #include "exception/data_error.h"  
上面的diff結果表明

1.某個提交記錄0c5ee代表的socket_helper.cpp檔案是原始檔,當前working area的socket_helper檔案是目標檔案。

2.在原始檔第4行開始的6行和目標檔案第4行開始的7行構成一個差異小結

3.這個差異小結中,目標檔案添加了一行#include "helper/time_measure.h"

4.其他空格開頭的行表明沒有差異。