以下git命令有什么区别?
git diff HEAD
git diff HEAD^
git diff --cached
或同义词git diff --staged
git diff
答案 0 :(得分:112)
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^ <--------------------+
答案 1 :(得分:10)
<强> git diff
强>
将显示工作目录中尚未为下一次提交暂存的更改。
<强> git diff --cached
强>
将显示索引与上次提交之间的区别;如果你在没有“-a”选项的情况下运行“git commit”,你会提交什么。
<强> git diff HEAD
强>
显示自上次提交以来工作目录中的更改;如果你运行“git commit -a”,你会提交什么。
答案 2 :(得分:9)
git diff HEAD
:HEAD与工作目录之间的差异。git diff HEAD^
:HEAD的直接祖先和工作目录之间的差异。git diff --cached
或同义词git diff --staged
:HEAD与索引之间的差异。git diff
:索引和工作目录之间的差异。答案 3 :(得分:5)
这是记住这些命令的简单方法:
git diff
的来源被认为是有效的
目录,其目标是索引。--cached
标志会将源更改为索引。一旦添加更多参数,它不一定会更改目标。但为方便起见,如果没有提供目标,则目标成为最后一次提交。根据自己的喜好将它们混合起来:)
答案 4 :(得分:0)
HEAD
是树中的当前HEAD
指针,HEAD^
是HEAD
之前的提交。 --cached
我不确定。--cached
会向您显示您所做的任何更改但尚未添加到索引中。
kernal.org上的git tutorial非常好读。