这些 git diff 命令之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3686452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between these git diff commands?
提问by Matthew Rankin
What are the differences between the following git commands?
以下 git 命令之间有什么区别?
git diff HEAD
git diff HEAD^
git diff --cached
or the synonymgit diff --staged
git diff
git diff HEAD
git diff HEAD^
git diff --cached
或同义词git diff --staged
git diff
回答by Amber
git diff HEAD
- Shows what has changed since the last commit.git diff HEAD^
- Shows what has changed since the commit beforethe latest commit.git diff --cached
- Show what has been added to the index viagit add
but not yet committed.git diff
- Show what has changed but hasn't been added to the index yet viagit add
.
git diff HEAD
- 显示自上次提交以来发生的变化。git diff HEAD^
- 显示自最近一次提交之前提交以来发生的变化。git diff --cached
- 显示已通过git add
但尚未提交的内容添加到索引中。git diff
- 通过 显示已更改但尚未添加到索引中的内容git add
。
It looks like this:
它看起来像这样:
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^ <--------------------+
回答by Matthew Rankin
From the Git Community Book:
来自Git 社区手册:
git diff
git diff
will show you changes in the working directory that are not yet staged for the next commit.
将显示工作目录中尚未为下一次提交暂存的更改。
git diff --cached
git diff --cached
will show you the difference between the index and your last commit; what you would be committing if you run "git commit" without the "-a" option.
将显示索引和上次提交之间的差异;如果您在没有“-a”选项的情况下运行“git commit”,您将提交什么。
git diff HEAD
git diff HEAD
shows changes in the working directory since your last commit; what you would be committing if you run "git commit -a".
显示自上次提交以来工作目录中的更改;如果您运行“git commit -a”,您将提交什么。
回答by wRAR
git diff HEAD
: Diff between HEAD and the working directory.git diff HEAD^
: Diff between the direct ancestor of HEAD and the working directory.git diff --cached
or the synonymgit diff --staged
: Diff between HEAD and the index.git diff
: Diff between the index and the working directory.
git diff HEAD
: HEAD 和工作目录之间的差异。git diff HEAD^
: HEAD 的直接祖先和工作目录之间的差异。git diff --cached
或同义词git diff --staged
:HEAD 和索引之间的差异。git diff
: 索引和工作目录之间的差异。
回答by gunby
Here's a simple way to remember these commands:
这是记住这些命令的简单方法:
- By default,
git diff
's source is assumed to be the working directory, and its target is the index. - Adding the
--cached
flag changes the source to be the index. It doesn't necessarily change the target once you add more arguments. But for convenience the target becomes the last commit if none is provided. - Adding a commit as an argument changes the target.
- Adding two commits as arguments changes both the source and the target, respectively.
- 默认情况下,
git diff
的源被假定为工作目录,其目标是索引。 - 添加
--cached
标志会将源更改为索引。添加更多参数后,它不一定会更改目标。但是为了方便起见,如果没有提供,则目标将成为最后一次提交。 - 添加提交作为参数会更改目标。
- 添加两个提交作为参数分别更改源和目标。
Have fun mixing them up to your own liking :)
根据自己的喜好将它们混合在一起玩得开心:)
回答by Josh K
HEAD
is the current HEAD
pointer in the tree, HEAD^
is the commit before HEAD
. --cached
I'm not sure about.--cached
will show you any changes you have made but haven't added to the index.
HEAD
是HEAD
树中的当前指针,HEAD^
是之前的提交HEAD
。--cached
我不确定。--cached
将显示您所做但尚未添加到索引中的任何更改。
The git tutorialon kernal.org is a very good read.
kernal.org 上的git 教程是一本非常好的读物。