当前位置: 首页 > 工具软件 > gitstatus > 使用案例 >

git status 命令总结 —— Git 学习笔记 06

裴星洲
2023-12-01

当执行 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 高手之路》,人民邮电出版社

 类似资料: