简体   繁体   English

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

[英]See diff between current state and last commit

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?如何查看代码当前状态和上次提交的差异?

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 diff and git diff --cached .这些更改是git diffgit diff --cached的组合。

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.这会将 HEAD 与之前的提交进行比较。 One could also do一个也可以做

git diff HEAD^^

to compare to the state of play 2 commits ago.与 2 次提交前的游戏状态进行比较。 To see the diff between the current state and a certain commit, just simply do:要查看当前状态和某个提交之间的差异,只需执行以下操作:

git diff b6af6qc

Where b6af6qc is an example of a commit hash.其中b6af6qc是提交哈希的示例。

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)).显示索引文件和当前 HEAD 提交之间存在差异的路径,工作树和索引文件之间存在差异的路径,以及工作树中未被 git 跟踪的路径(并且不被 gitignore(5) 忽略) )。 The first are what you would commit by running git commit;第一个是您将通过运行 git commit 提交的内容; the second and third are what you could commit by running git add before running 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

You ask git to diff the current/last commit, which has a shorthand of HEAD .您要求 git 区分当前/最后一次提交,它的简写为HEAD

So git diff HEAD will compare the current state of the worktree with the current commit.所以git diff HEAD会将工作树的当前状态与当前提交进行比较。

This also works for me:这也适用于我:

# The last one
git diff HEAD~1 HEAD

# The last but one, etc...
git diff HEAD~2 HEAD~1

This usually works for a linear history.这通常适用于线性历史。 This could get more tricky if there are also merge commits.如果还有合并提交,这可能会变得更加棘手。 I recommend you to look into this doc for a nice and complete explanation, especially that commit tree illustration example:我建议你查看这个文档以获得一个很好和完整的解释,特别是那个提交树图示例:

https://git-scm.com/docs/gitrevisions https://git-scm.com/docs/gitrevisions

Have you ever tried git show ?你有没有试过git show

DESCRIPTION : Shows one or more objects (blobs, trees, tags and commits).描述:显示一个或多个对象(斑点、树、标签和提交)。

For commits it shows the log message and textual diff.对于提交,它显示日志消息和文本差异。 It also presents the merge commit in a special format as produced by git diff-tree --cc.它还以由 git diff-tree --cc 生成的特殊格式呈现合并提交。

taken from git help取自 git help

You don't need to write HEAD or the SHA-1 of the last commit, only type git show .您不需要编写 HEAD 或上次提交的 SHA-1,只需键入git show

I think that it would be helpful for your needs as well as the other answers but with a little less typing and more information depending on the case.我认为这对您的需求以及其他答案都有帮助,但根据具体情况,可以减少打字次数和提供更多信息。

Here I add a sample of what git show actually shows:在这里,我添加了git show实际显示的示例:

>> git show

commit 49832d33b5329fff95ba0a86002ee8d5a762f3ae (HEAD -> my_new_branch, master)
Author: Abimael Domínguez <my_mail@mail.com>
Date:   Thu Jan 7 13:05:38 2021 -0600

    This is the commit message of the last commit

diff --git a/some_folder/some_file.txt b/some_folder/some_file.txt
index 59fb665..5c36cde 100644
--- a/some_folder/some_file.txt
+++ b/some_folder/some_file.txt
@@ -3,6 +3,6 @@
 This is the content of the last updated file
 some text
 some text
-text deleted
+text added
 some text
 some text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM