这些git diff命令有什么区别?

以下git命令有什么区别?

  1. git diff HEAD
  2. git diff HEAD^
  3. git diff --cached或同义词git diff --staged
  4. git diff
  1. git diff HEAD – 显示自上次提交以来更改的内容。
  2. git diff HEAD^ – 显示自从最近一次提交之前的提交后发生了什么变化。
  3. git diff --cached – 通过git add显示已添加到索引的内容,但尚未提交。
  4. 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社区图书 :

git diff

会显示工作目录中尚未执行下一个提交的更改。

git diff --cached

会告诉你索引和上次提交之间的区别; 如果你运行“git commit”而没有“-a”选项,你将会提交什么。

git diff HEAD

显示自上次提交以来工作目录中的更改; 如果你运行“git commit -a”你会提交什么。

  1. git diff HEAD :HEAD和工作目录之间的git diff HEAD
  2. git diff HEAD^git diff HEAD^的直接祖先和工作目录之间的git diff HEAD^
  3. git diff --cached或同义词git diff --staged :HEAD和索引之间的git diff --staged
  4. git diff :索引和工作目录之间的git diff

以下是记住这些命令的简单方法:

  • 默认情况下, git diff的源被假定为工作目录,其目标是索引。
  • 添加--cached标志会将源更改为索引。 一旦添加更多参数,它不一定会改变目标。 但是为了方便,如果没有提供,目标将成为最后的提交。
  • 添加提交作为参数改变目标。
  • 添加两个提交作为参数分别改变源和目标。

玩得开心,把它们混合到你自己喜欢的地方:)

HEAD是树中当前的HEAD指针, HEAD^HEAD之前的提交。 --cached我不确定。 --cached将显示您所做的任何更改,但尚未添加到索引中。

kernal.org上的git教程是一个很好的阅读。