git diff 文件针对其上次更改

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

git diff file against its last change

gitgit-diff

提问by Chowlett

Is it possible to get git to produce a diff between a specific file as it exists now, and as it existed before the last commit that changed it?

是否有可能让 git 在特定文件之间产生差异,因为它现在存在,因为它在上次提交更改它之前就存在?

That is, if we know:

也就是说,如果我们知道:

$ git log --oneline myfile
123abc Fix some stuff
456def Frobble the foos
789dba Initial commit

Then git diff 456def myfileshows the last change to myfile. Is is possible to do the same without the knowledge produced by the git log; what changed in 123abc?

然后git diff 456def myfile显示对 myfile 的最后更改。是否有可能在没有产生的知识的情况下做同样的事情git log;123abc 有什么变化?

采纳答案by Kyle Strand

This does exist, but it's actually a feature of git log:

这确实存在,但它实际上是一个特性git log

git log -p [--follow] [-1] <path>

Note that -pcan also be used to show the inline diff from a single commit:

请注意,-p也可用于显示来自单个提交的内联差异:

git log -p -1 <commit>

Options used:

使用的选项:

  • -p(also -uor --patch) is hidden deeeeeeeep in the git-logman page, and is actually a display option for git-diff. When used with log, it shows the patch that would be generated for each commit, along with the commit information—and hidescommits that do not touch the specified <path>. (This behavior is described in the paragraph on --full-diff, which causes the full diff of each commit to be shown.)
  • -1shows justthe most recent change to the specified file(-n 1can be used instead of -1); otherwise, allnon-zero diffs of that file are shown.
  • --followis required to see changes that occurred prior to a rename.
  • -p(also-u--patch) 在git-log手册页中隐藏 deeeeeeeep,实际上是git-diff. 与 一起使用时log,它显示将为每个提交生成的补丁以及提交信息——并隐藏不涉及指定<path>. (此行为在 上的段落中进行了描述--full-diff,这会导致显示每个提交的完整差异。)
  • -1显示对指定文件的最新更改(-n 1可以使用 代替-1);否则,将显示该文件的所有非零差异。
  • --follow需要查看重命名之前发生的更改。

As far as I can tell, this is the only way to immediately see the last set of changes made to a file without using git log(or similar) to either count the number of intervening revisions or determine the hash of the commit.

据我所知,这是立即查看对文件所做的最后一组更改的唯一方法,而无需使用git log(或类似方法)计算干预修订的数量或确定提交的哈希值。

To see older revisions changes, just scroll through the log, or specify a commit or tag from which to start the log. (Of course, specifying a commit or tag returns you to the original problem of figuring out what the correct commit or tag is.)

要查看较旧的修订更改,只需滚动日志,或指定从其开始日志的提交或标记。(当然,指定提交或标记会使您返回到找出正确提交或标记是什么的原始问题。)

Credit where credit is due:

信用到期的信用:

  • I discovered log -pthanks to this answer.
  • Credit to FranciscoPuga and this answerfor showing me the --followoption.
  • Credit to ChrisBetti for mentioning the -n 1option and atatko for mentioning the -1variant.
  • Credit to sweaver2112 for getting me to actually read the documentation and figure out what -p"means" semantically.
  • 我发现log -p感谢这个答案
  • 感谢FranciscoPuga 和这个向我展示--follow选项的答案
  • 感谢 ChrisBetti 提到的-n 1选项和 atatko 提到的-1变体。
  • 感谢 sweaver2112 让我真正阅读文档并找出-p语义上的“含义”。

回答by Francisco Puga

One of the ways to use git diff is:

使用 git diff 的方法之一是:

git diff <commit> <path>

And a common way to refer one commit of the last commit is as a relative path to the actual HEAD. You can reference previous commits as HEAD^ (in your example this will be 123abc) or HEAD^^ (456def in your example), etc ...

引用最后一次提交的一个提交的常用方法是作为实际 HEAD 的相对路径。您可以将以前的提交引用为 HEAD^(在您的示例中为 123abc)或 HEAD^^(在您的示例中为 456def)等......

So the answer to your question is:

所以你的问题的答案是:

git diff HEAD^^ myfile

回答by Martin G

If you are fine using a graphical tool this works very well:

如果您可以很好地使用图形工具,这将非常有效:

gitk <file>

gitk now shows all commits where the file has been updated. Marking a commit will show you the diff against the previous commit in the list. This also works for directories, but then you also get to select the file to diff for the selected commit. Super useful!

gitk 现在显示文件已更新的所有提交。标记提交将显示与列表中上一个提交的差异。这也适用于目录,但是您还可以选择要针对所选提交进行差异的文件。超级好用!