git 查看文件先前版本差异的速记

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

Shorthand to view diff of previous version of file

gitdiff

提问by Clayton

I can easily find out what changed for a file since the last commit with git diff HEAD^ -- <filename>but is there an equivalent shorthand to view a diff for a particular file since it was last committed, regardless of how many commits have happened since? Or to go back N commits of that particular file?

我可以很容易地找出自上次提交以来文件发生了什么变化,git diff HEAD^ -- <filename>是否有等效的速记来查看自上次提交以来特定文件的差异,无论此后发生了多少次提交?或者返回该特定文件的 N 次提交?

Context:I found an error in a file and I want to track down when it snuck in. It's easy enough to get a log report for a particular file with git log -<n> <filename>to show only the commits that included changes to that file. So clearly I can just copy and paste the SHAs from that logreport, but what I really want is to be able to do something like git diff ^ -- <filename>or git diff ~2 -- <filename>.

上下文:我在一个文件中发现了一个错误,我想在它潜入时进行跟踪。获取特定文件的日志报告并git log -<n> <filename>仅显示包含对该文件的更改的提交很容易。很明显,我可以从该log报告中复制和粘贴 SHA ,但我真正想要的是能够执行诸如git diff ^ -- <filename>或 之类的操作git diff ~2 -- <filename>

回答by Matt McHenry

$ git log -p <filename>

will show you the log message plus a diff for each commit that touched the named file.

将向您显示日志消息以及触及命名文件的每个提交的差异。

To show only differences to the previous version, ask for just one step in log history:

要仅显示与先前版本的差异,请在日志历史记录中仅要求一个步骤:

$ git log -1 -p <filename>

回答by Alex W

You can make use of git logformatting to get the hashes of previous commits to a file. For example,

您可以使用git log格式来获取以前提交到文件的哈希值。例如,

git log --pretty=format:'%h' -1 --skip=1 <filename>

will get you the 2nd to last commit to touch a specific file. In fact, if you don't specify a filename, this will get you the 2nd to last commit on the entire repository. To get older hashes, you might set up a git alias that calls a shell function, like this:

将使您成为倒数第二次接触特定文件的提交。事实上,如果您不指定文件名,这将使您在整个存储库中进行倒数第二次提交。要获得较旧的哈希值,您可以设置一个调用 shell 函数的 git 别名,如下所示:

[alias]
    prior-hash = "!ph() { git log --pretty=format:'%h' -1 --skip= ; }; ph"

To use it, you'd type something like git prior-hash n <filename>, where n is the (n+1)th most recent version of the file. So 1 would be the 2nd to last commit to the file, 2 would be the 3rd to last, etc, and 0 would be the most recent commit to touch that file. And again, the filename is optional, if you want to examine the repo as a whole.

要使用它,您需要输入类似的内容git prior-hash n <filename>,其中 n 是文件的第 (n+1) 个最新版本。因此,1 将是对文件的倒数第二次提交,2 将是倒数第三次,依此类推,而 0 将是触摸该文件的最近一次提交。再说一次,如果您想从整体上检查存储库,文件名是可选的。

I'm sure you could figure out how to build a diff command from there:

我相信您可以弄清楚如何从那里构建 diff 命令:

[alias]
    prior-hash = "!ph() { git log --pretty=format:'%h' -1 --skip= ; }; ph"
    diff-prev-easy = "!dp() { git diff $(git prior-hash  ).. ; }; dp"

which would be used similar to the prior-hash alias, git diff-prev-easy n <filename>. This would compare the (n+1)th last revision to the most recent revision of the file. If you wanted to instead compare the (n+1)th last revision to the nth last revision, it's a simple change:

这将类似于先验哈希别名git diff-prev-easy n <filename>. 这会将第 (n+1) 个最后修订版与文件的最新修订版进行比较。如果您想将第 (n+1) 个最后一个修订版与第 n 个最后一个修订版进行比较,这是一个简单的更改:

[alias]
    prior-hash = "!ph() { git log --pretty=format:'%h' -1 --skip= ; }; ph"
    diff-prev = "!dp() { git diff $(git prior-hash  )..$(git prior-hash $(( - 1)) ) ; }; dp"

which, again, is used the same way: git diff-prev n <filename>

再次以相同的方式使用: git diff-prev n <filename>

One potential problem to watch out for, though, is that git loglists commits in chronological order, which may not be what you want. Consider this history:

但是,需要注意的一个潜在问题是git log按时间顺序列出提交,这可能不是您想要的。想想这段历史:

1 - 2 - - - 4 - 5   master
      \       /
        3 - -       develop

Our git diff-prev 1command would produce the diff between commit 4 and 5, as expected. But git diff-prev 2would show the diff between commit 3 and 4, which is likely undesirable.

git diff-prev 1正如预期的那样,我们的命令将在提交 4 和 5 之间产生差异。但是git diff-prev 2会显示提交 3 和 4 之间的差异,这可能是不可取的。

回答by Mike Monkiewicz

git blameshould get you to your destination pretty fast.

git blame应该可以让你很快到达目的地。