git 查看当前状态和上次提交之间的差异

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

See diff between current state and last commit

git

提问by travis1097

Sometimes when I'm about to make a commit, I can't recall exactly what has changed since the last commit. How can I see a diff of the current state of the code and the last commit?

有时,当我要进行提交时,我不记得自上次提交以来到底发生了什么变化。如何查看代码当前状态和上次提交的差异?

回答by Klas Mellbourn

If you haven't added any files to the index yet (with git add), simply do

如果您尚未向索引添加任何文件(使用git add),只需执行

git diff

This will show the diff between your working tree and index.

这将显示您的工作树和索引之间的差异。

If you have added files to the index, you need to do this to show the differences between index and the last commit (HEAD).

如果您已将文件添加到索引,则需要执行此操作以显示索引与上次提交 (HEAD) 之间的差异。

git diff --cached

Finally, if you want to see the changes made in the working tree compared to the latest commit (HEAD) you can (as Carlos points out) do

最后,如果您想查看与最新提交 ( HEAD)相比在工作树中所做的更改,您可以(正如 Carlos 指出的那样)

git diff HEAD

Those changes are the combination of git diffand git diff --cached.

这些变化的组合git diffgit diff --cached

回答by tallamjr

If you have just made a commit, or want to see what has changed in the last commit compared to the current state (assuming you have a clean working tree) you can use:

如果您刚刚进行了一次提交,或者想查看与当前状态相比在上次提交中发生了什么变化(假设您有一个干净的工作树),您可以使用:

git diff HEAD^

This will compare the HEAD with the commit immediately prior. One could also do

这会将 HEAD 与之前的提交进行比较。一个也可以做

git diff HEAD^^

to compare to the state of play 2 commits ago. To see the diff between the current state and a certain commit, just simply do:

与 2 次提交前的游戏状态进行比较。要查看当前状态和某个提交之间的差异,只需执行以下操作:

git diff b6af6qc

Where b6af6qcis an example of a commit hash.

b6af6qc提交哈希的示例在哪里。

回答by Carlos Martín Nieto

You ask git to diff the current/last commit, which has a shorthand of HEAD.

您要求 git 区分当前/最后一次提交,它的简写为HEAD.

So git diff HEADwill compare the current state of the worktree with the current commit.

所以git diff HEAD会将工作树的当前状态与当前提交进行比较。

回答by dekdev

this also shows the difference and what files has been changed/modified.

这也显示了差异以及哪些文件已被更改/修改。

$ git Status 

Displays paths that have differences between the index file and the current HEAD commit, paths that have differences between the working tree and the index file, and paths in the working tree that are not tracked by git (and are not ignored by gitignore(5)). The first are what you would commit by running git commit; the second and third are what you could commit by running git add before running git commit.

显示索引文件和当前 HEAD 提交之间存在差异的路径、工作树和索引文件之间存在差异的路径以及工作树中未被 git 跟踪的路径(并且不被 gitignore(5) 忽略) )。第一个是您将通过运行 git commit 提交的内容;第二个和第三个是你可以通过在运行 git commit 之前运行 git add 来提交的内容。

https://www.kernel.org/pub/software/scm/git/docs/git-status.html

https://www.kernel.org/pub/software/scm/git/docs/git-status.html