以下 git 命令有什么区别?
git diff HEAD
git diff HEAD^
git diff --cached
或同义词git diff --staged
git diff
git diff HEAD
- 显示自上次提交以来发生的变化。git diff HEAD^
- 显示自上次提交之前的提交以来发生了什么变化。git diff --cached
- 显示已通过git add
但尚未提交的内容添加到索引中。git diff
- 通过显示已更改但尚未添加到索引中的内容git add
。它看起来像这样:
Working
Directory <----+--------+------+
| | | |
| | | |
V | | |
"git add" | | |
| diff | |
| | | |
V | | |
Index <----+ diff HEAD |
| | | |
| | | |
V | | |
"git commit" | | |
| diff --cached | |
| diff --staged | |
V | | |
HEAD <----+--------+ |
| |
| diff HEAD^
V |
previous "git commit" |
| |
| |
V |
HEAD^ <--------------------+
git diff HEAD
: HEAD 和工作目录之间的区别。git diff HEAD^
: HEAD 的直接祖先和工作目录之间的差异。git diff --cached
或同义词git diff --staged
:HEAD 和索引之间的差异。git diff
: 索引和工作目录之间的差异。来自Git 社区手册:
git diff
将显示工作目录中尚未为下一次提交暂存的更改。
git diff --cached
将向您显示索引和上次提交之间的差异;如果您在没有“-a”选项的情况下运行“git commit”,您将提交什么。
git diff HEAD
显示自上次提交以来工作目录的更改;如果你运行“git commit -a”你会提交什么。
这是记住这些命令的简单方法:
git diff
假定源为工作目录,目标为索引。--cached
标志将源更改为索引。添加更多参数后,它不一定会更改目标。但是为了方便起见,如果没有提供,目标将成为最后一次提交。玩得开心把它们混合成你自己的喜好:)
HEAD
HEAD
是树中的当前指针,HEAD^
是之前的提交HEAD
。--cached
我不确定。--cached
将显示您所做但尚未添加到索引中的任何更改。
kernal.org 上的git 教程非常好读。