git status 命令總結 —— Git 學習筆記 06

ARM的程式設計師敲著詩歌的夢發表於2020-04-04

當執行 git status 的時候,返回結果大致可分為3個部分:

  1. 擬提交的變更:這是已經放入暫存區,準備使用 git commit 命令提交的變更
  2. 未暫存的變更:這是工作目錄和暫存區快照之間存在差異的檔案列表
  3. 未跟蹤的檔案:這類檔案對於 Git 系統來說是未知的,也是可以被忽略的

如果在 git status 命令後面加上 --ignored選項,還會列出被忽略的檔案。
例如:

$ git status --ignored
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working\
 directory)
Untracked files:
  (use "git add <file>..." to include in what will be committed)
Ignored files:
  (use "git add -f <file>..." to include in what will be committed)

還有一種簡潔的輸出格式,即新增 --short 選項,例如

$ git status --short
 D apple.c
 M hello.c
A  world.c
AD world_bak.c
?? 123.txt

git status --short也可以簡單寫成 git status -s

這種輸出每一行的格式是

XY PATH1 -> PATH2

PATH1 表示最近一次提交的檔案, -> PATH2表示索引或工作目錄中檔案,當檔案路徑改變時才會有 -> PATH2這一項。

XY都是狀態碼,X表示暫存區和最近一次提交的差異,Y表示工作目錄和暫存區的差異。

其含義是:

  • ’ ’ = unmodified
  • M = modified
  • A = added
  • D = deleted
  • R = renamed
  • C = copied
  • U = updated but unmerged

預設不會列出被忽略的檔案,除非使用 --ignored選項。

XY可能的組合如下表(方括號裡面的可以沒有):

X          Y     Meaning
-------------------------------------------------
                 not updated
M        [ MD]   updated in index
A        [ MD]   added to index
D                deleted from index
R        [ MD]   renamed in index
C        [ MD]   copied in index
[MARC]           index and work tree matches
[ MARC]     M    work tree changed since index
[ MARC]     D    deleted in work tree
[ D]        R    renamed in work tree
[ D]        C    copied in work tree
-------------------------------------------------
D           D    unmerged, both deleted
A           U    unmerged, added by us
U           D    unmerged, deleted by them
U           A    unmerged, added by them
D           U    unmerged, deleted by us
A           A    unmerged, both added
U           U    unmerged, both modified
-------------------------------------------------
?           ?    untracked
!           !    ignored
-------------------------------------------------


參考資料
【1】https://git-scm.com/docs/git-status
【2】《Git 高手之路》,人民郵電出版社

相關文章