这些 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:33:41  来源:igfitidea点击:

What are the differences between these git diff commands?

gitgit-diff

提问by Matthew Rankin

What are the differences between the following git commands?

以下 git 命令之间有什么区别?

  1. git diff HEAD
  2. git diff HEAD^
  3. git diff --cachedor the synonym git diff --staged
  4. git diff
  1. git diff HEAD
  2. git diff HEAD^
  3. git diff --cached或同义词 git diff --staged
  4. git diff

回答by Amber

  1. git diff HEAD- Shows what has changed since the last commit.
  2. git diff HEAD^- Shows what has changed since the commit beforethe latest commit.
  3. git diff --cached- Show what has been added to the index via git addbut not yet committed.
  4. git diff- Show what has changed but hasn't been added to the index yet via git add.
  1. git diff HEAD- 显示自上次提交以来发生的变化。
  2. git diff HEAD^- 显示自最近一次提交之前提交以来发生的变化。
  3. git diff --cached- 显示已通过git add但尚未提交的内容添加到索引中。
  4. 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

  1. git diff HEAD: Diff between HEAD and the working directory.
  2. git diff HEAD^: Diff between the direct ancestor of HEAD and the working directory.
  3. git diff --cachedor the synonym git diff --staged: Diff between HEAD and the index.
  4. git diff: Diff between the index and the working directory.
  1. git diff HEAD: HEAD 和工作目录之间的差异。
  2. git diff HEAD^: HEAD 的直接祖先和工作目录之间的差异。
  3. git diff --cached或同义词git diff --staged:HEAD 和索引之间的差异。
  4. 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 --cachedflag 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

HEADis the current HEADpointer in the tree, HEAD^is the commit before HEAD. --cachedI'm not sure about.--cachedwill show you any changes you have made but haven't added to the index.

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

The git tutorialon kernal.org is a very good read.

kernal.org 上的git 教程是一本非常好的读物。