git diff HEAD 与 git diff --staged 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16562121/
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-10 16:10:03  来源:igfitidea点击:

What is the difference between git diff HEAD vs. git diff --staged?

git

提问by vhd

What is the difference between git diff HEADand git diff --staged? I tried both but both give the same output.

git diff HEAD和 和有git diff --staged什么区别?我都试过,但都给出了相同的输出。

回答by Carlos Campderrós

Suppose this output for git status:

假设此输出为git status

$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   y
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   x
#

As you see, there is one file modified but not staged for commit, and a new file added that is ready to be committed.

如您所见,有一个文件已修改但未暂存以进行提交,并添加了一个准备提交的新文件。

git diff --stagedwill only show changes to files in the "staged" area.

git diff --staged只会显示“暂存”区域中文件的更改。

git diff HEADwill show all changes to tracked files. If you have all changes staged for commit, then both commands will output the same.

git diff HEAD将显示对跟踪文件的所有更改。如果您已将所有更改暂存为提交,则两个命令将输出相同的结果。

回答by Premraj

  • git diffView difference between Stage and Working Directory
  • git diff --stagedView difference between HEAD and Stage
  • git diff HEADView difference between HEAD and Working Directory
  • git diff查看阶段和工作目录之间的区别
  • git diff --staged查看 HEAD 和 Stage 的区别
  • git diff HEAD查看 HEAD 和工作目录之间的区别

enter image description here
img src

在此处输入图片说明
图像源

enter image description hereenter image description here
img src

在此处输入图片说明在此处输入图片说明
图像源

  • originrefers to the source repository from where it was cloned.
  • HEADis a reference to the last commit in the currently checked-out branch.
  • Staged and index both are same
  • Unstaged changes exist in our Working Directory, but Git hasn't recorded them into its version history yet.
  • Staged changes are a lot like unstaged changes, except that they've been marked to be committed the next time you run git commit
  • origin指从中克隆它的源存储库。
  • HEAD是对当前签出分支中最后一次提交的引用。
  • Staged 和 index 都是一样的
  • 我们的工作目录中存在未暂存的更改,但 Git 尚未将它们记录到其版本历史中。
  • 阶段性更改很像非阶段性更改,只是它们已被标记为在您下次运行 git commit 时提交

回答by VonC

You will be able to see more easily the difference between the two diff with the upcomming (Git 2.3.4+, Q2 2015) git status -v -v

您将能够更轻松地看到两个差异之间的差异(Git 2.3.4+,2015 年第二季度) git status -v -v

See commit 4055500from Michael J Gruber mjg, it does a good job explaining the difference between git diff HEADand git diff --staged:

犯4055500迈克尔·格鲁伯Ĵmjg,它很好地解释之间的区别git diff HEADgit diff --staged

commit/status: show the index-worktree diff with -v -v

git commitand git statusin long format show the diff between HEAD and the index when given -v. This allows previewing a commit to be made.

They also list tracked files with unstaged changes, but without a diff.

Introduce '-v -v' which shows the diff between the index and the worktree in additionto the HEADindex diff. This allows a review of unstaged changes which might be missing from the commit.

In the case of '-v -v', additional header lines

commit/ status: 显示索引工作树差异-v -v

git commitgit status以长格式显示 HEAD 和给定索引之间的差异-v。这允许预览要进行的提交

他们还列出了具有未暂存更改但没有差异的跟踪文件。

介绍“ -v -v”示出的索引和worktree之间的差异除了HEAD索引DIFF。这允许提交中可能缺少的未暂存更改

在“ -v -v”的情况下,附加标题行

Changes to be committed:
# and
Changes not staged for commit:

are inserted before the diffs, which are equal to those in the status part; the latter preceded by 50*"-" to make it stick out more.

插入差异之前,与状态部分中的差异相同;后者前面有 50*" -" 以使其更加突出。

回答by Bimme

You can also get different output from the two diff commands if you are running them on an unsuccessful merge, i.e. unmerged paths.

如果您在不成功的合并(即未合并的路径)上运行这两个 diff 命令,您还可以从这两个 diff 命令获得不同的输出。

I have not been able to find out why it behaves like this, but you could read more about it here

我一直无法找出它为什么会这样,但你可以在这里阅读更多关于它的信息