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
What is the difference between git diff HEAD vs. git diff --staged?
提问by vhd
What is the difference between git diff HEAD
and 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 --staged
will only show changes to files in the "staged" area.
git diff --staged
只会显示“暂存”区域中文件的更改。
git diff HEAD
will 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 diff
View difference between Stage and Working Directorygit diff --staged
View difference between HEAD and Stagegit diff HEAD
View difference between HEAD and Working Directory
git diff
查看阶段和工作目录之间的区别git diff --staged
查看 HEAD 和 Stage 的区别git diff HEAD
查看 HEAD 和工作目录之间的区别
origin
refers to the source repository from where it was cloned.HEAD
is 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 HEAD
and git diff --staged
:
见犯4055500从迈克尔·格鲁伯Ĵmjg
,它很好地解释之间的区别git diff HEAD
和git diff --staged
:
commit
/status
: show the index-worktree diff with-v -v
git commit
andgit status
in 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 theHEAD
index 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 commit
并git 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