1

git 命令 ——git status、git diff

 1 year ago
source link: https://blog.p2hp.com/archives/9411
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

git 命令 ——git status、git diff

前言

当对项目做了更改时,我们通常需要知道具体改了哪些文件,哪些文件更改了没有暂存,哪些文件改了并且已加入到暂存区等待下次 commit。上述任务使用 git status 都可以帮我们解决。但是想要知道文件内部改了哪些地方 git status 就无能为力了。git status 最多只告诉你改没改,改哪了不知道。git diff 可以解决这个问题。

git status

git status 命令的输出十分详细,但其用语有些繁琐。 如果你使用 git status -s 命令或 git status --short 命令,你将得到一种更为紧凑的格式输出。 运行 git status -s ,状态报告输出如下:

$ git status -s
 M README
MM Rakefile
A  lib/git.rb
M  lib/simplegit.rb
?? LICENSE.txt

右侧 M:该文件被修改了但是还没放入暂存区

左侧 M:该文件被修改了且放入暂存区

MM:在工作区被修改并提交到暂存区后又在工作区中被修改了,所以在暂存区和工作区都有该文件被修改了的记录。

A:新添加到暂存区中的文件

??:新添加的未跟踪文件

X          Y     Meaning
-------------------------------------------------
          [MD]   not updated
M        [ MD]   updated in index
A        [ MD]   added to index
D         [ M]   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           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
-------------------------------------------------

git diff

参考:Linux 命令 ——diff

不加参数直接输入 git diff

工作目录里面的文件 与 staging area 里面的文件做对比。既然 staging area 里面有这个文件,那么他一定是 tracked 的。

git diff 本身只显示尚未暂存的改动,而不是自上次提交以来所做的所有改动。 假设这样一种场景,你 clone 了一个项目,改了 100 个文件,然后 git add 把他们加到暂存区。然后你 git diff,会什么也看不到。因为此时 staging area 有 100 个文件,但是你工作目录并没有再次修改那 100 个文件。

退一步讲,你 clone 了一个项目,改了 100 个文件。这时候你如果不 git add,直接 git diff 则是可以看到输出的。这时候 staging area 里面的内容默认是上次 cmooit 分支的内容。

git diff --cached

Git 1.6.1 及更高版本还允许使用 git diff --staged,效果是相同的,但更好记些。

如果你把修改文件加到 staging area,准备 commit。在 commit 之前,想看看我这次提交的内容和上个 commit 有啥差异,可以执行这个命令。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK