是否有可能让git在特定文件之间产生一个差异,因为它现在存在,因为它在最后一次提交之前存在,改变了它?
就是说,如果我们知道:
$ git log --oneline myfile
123abc Fix some stuff
456def Frobble the foos
789dba Initial commit
然后git diff 456def myfile显示myfile的最后一次更改。在没有git日志产生的知识的情况下做同样的事情是可能的;123abc发生了什么变化?
是否有可能让git在特定文件之间产生一个差异,因为它现在存在,因为它在最后一次提交之前存在,改变了它?
就是说,如果我们知道:
$ git log --oneline myfile
123abc Fix some stuff
456def Frobble the foos
789dba Initial commit
然后git diff 456def myfile显示myfile的最后一次更改。在没有git日志产生的知识的情况下做同样的事情是可能的;123abc发生了什么变化?
当前回答
这确实存在,但它实际上是git log的一个特性:
git log -p [-m] [--follow] [-1] <path>
注意-p也可以用来显示单次提交的内联差异:
git log -p -1 <commit>
选择使用:
-p (also -u or --patch) is hidden deeeeeeeep in the git-log man 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 hides commits 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.) -m causes merge commits to include the diff content (otherwise these just show the commit message, as if -p were not specified). -1 shows just the most recent change to the specified file (-n 1 can be used instead of -1); otherwise, all non-zero diffs of that file are shown. --follow is required to see changes that occurred prior to a rename.
据我所知,这是在不使用git log(或类似)来计算中间修订的数量或确定提交的哈希的情况下,立即查看对文件所做的最后一组更改的唯一方法。
若要查看较旧版本的更改,只需滚动日志,或指定开始日志的提交或标记。(当然,指定提交或标记会使您回到最初的问题,即确定正确的提交或标记是什么。)
应得的荣誉:
多亏了这个答案,我发现了log -p。 感谢FranciscoPuga和这个回答给我展示了-follow选项。 ChrisBetti提到了-n 1选项,atatko提到了-1变体。 感谢sweaver2112让我真正阅读文档并弄清楚-p在语义上“意味着”什么。 感谢Oscar Scholten指出,默认情况下,-p在合并提交时不显示diff-contents。
其他回答
使用git diff的一种方法是:
git diff <commit> <path>
引用上一次提交的一个提交的常用方法是作为实际HEAD的相对路径。你可以引用以前的提交HEAD^(在你的例子中这将是123abc)或HEAD^^(在你的例子中是456def),等等…
所以你问题的答案是
git diff HEAD^^ myfile
这确实存在,但它实际上是git log的一个特性:
git log -p [-m] [--follow] [-1] <path>
注意-p也可以用来显示单次提交的内联差异:
git log -p -1 <commit>
选择使用:
-p (also -u or --patch) is hidden deeeeeeeep in the git-log man 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 hides commits 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.) -m causes merge commits to include the diff content (otherwise these just show the commit message, as if -p were not specified). -1 shows just the most recent change to the specified file (-n 1 can be used instead of -1); otherwise, all non-zero diffs of that file are shown. --follow is required to see changes that occurred prior to a rename.
据我所知,这是在不使用git log(或类似)来计算中间修订的数量或确定提交的哈希的情况下,立即查看对文件所做的最后一组更改的唯一方法。
若要查看较旧版本的更改,只需滚动日志,或指定开始日志的提交或标记。(当然,指定提交或标记会使您回到最初的问题,即确定正确的提交或标记是什么。)
应得的荣誉:
多亏了这个答案,我发现了log -p。 感谢FranciscoPuga和这个回答给我展示了-follow选项。 ChrisBetti提到了-n 1选项,atatko提到了-1变体。 感谢sweaver2112让我真正阅读文档并弄清楚-p在语义上“意味着”什么。 感谢Oscar Scholten指出,默认情况下,-p在合并提交时不显示diff-contents。
如果你擅长使用图形化工具,这将非常有效:
gitk <file>
Gitk现在显示文件已更新的所有提交。标记一次提交将显示列表中与前一次提交的差异。这也适用于目录,但随后您还需要为所选提交选择要进行差异的文件。超级有用啊!