简体   繁体   English

git diff文件反对它的最后一次更改

[英]git diff file against its last change

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 myfile shows the last change to myfile. 然后git diff 456def myfile显示git diff 456def myfile的最后一次更改。 Is is possible to do the same without the knowledge produced by the git log ; 没有git log产生的知识,可以做同样的事情; what changed in 123abc? 123abc有什么变化?

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. 引用最后一次提交的一次提交的常用方法是作为实际HEAD的相对路径。 You can reference previous commits as HEAD^ (in your example this will be 123abc) or HEAD^^ (456def in your example), etc ... 您可以将以前的提交引用为HEAD ^(在您的示例中,这将是123abc)或HEAD ^^(在您的示例中为456def)等等...

So the answer to your question is: 所以你的问题的答案是:

git diff HEAD^^ myfile

This does exist, but it's actually a feature of git log : 这确实存在,但它实际上是git log一个特性:

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

Note that -p can also be used to show the inline diff from a single commit: 请注意, -p还可用于显示单个提交的内联差异:

git log -p -1 <commit>

Options used: 使用的选项:

  • -p (also -u or --patch ) is hidden deeeeeeeep in the git-log man page, and is actually a display option for git-diff . -p (也是-u--patch )在git-log手册页中隐藏了deeeeeeeep,实际上是git-diff的显示选项。 When used with log , it shows the patch that would be generated for each commit , along with the commit information—and hides commits that do not touch the specified <path> . log使用时,它会显示为每次提交生成的补丁,以及提交信息,并隐藏不接触指定<path>提交。 (This behavior is described in the paragraph on --full-diff , which causes the full diff of each commit to be shown.) (这种行为在--full-diff的段落中描述,它会导致显示每个提交的完整差异。)
  • -1 shows just the most recent change to the specified file ( -n 1 can be used instead of -1 ); -1表示只是最近改变到指定文件-n 1可以用来代替-1 ); otherwise, all non-zero diffs of that file are shown. 否则,显示该文件的所有非零差异。
  • --follow is required to see changes that occurred prior to a rename. --follow要求重新命名之前发生的更改需要--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 -p thanks to this answer . 由于这个答案,我发现了log -p
  • Credit to FranciscoPuga and this answer for showing me the --follow option. 感谢FranciscoPuga和这个答案向我展示了--follow选项。
  • Credit to ChrisBetti for mentioning the -n 1 option and atatko for mentioning the -1 variant. 感谢ChrisBetti提及-n 1选项和atatko提及-1变体。
  • Credit to sweaver2112 for getting me to actually read the documentation and figure out what -p "means" semantically. 感谢sweaver2112让我真正阅读文档,并找出-p “的意思是”语义。

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. gitk现在显示文件已更新的所有提交。 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. 这也适用于目录,但您也可以选择要为所选提交进行diff的文件。 Super useful! 超级有用!

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

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