git 如何在没有提交的情况下查看两次提交之间的更改?

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

How to see the changes between two commits without commits in-between?

gitdiff

提问by

How do you make git diffonly show the difference between two commits, excluding the other commits in-between?

你如何git diff只显示两次提交之间的差异,排除中间的其他提交?

回答by OneOfOne

you can simply pass the 2 commits to git diff like :

您可以简单地将 2 个提交传递给 git diff,例如:

-> git diff 0da94be  59ff30c > my.patch
-> git apply my.patch

回答by bdonlan

Asking for the difference /between/ two commits without including the commits in-between makes little sense. Commits are just snapshots of the contents of the repository; asking for the difference between two necessarily includes them. So the question then is, what are you really looking for?

询问/之间/两次提交的差异而不包括中间的提交是没有意义的。提交只是存储库内容的快照;要求两者之间的差异必然包括它们。那么问题来了,你真正在寻找什么?

As William suggested, cherry-picking can give you the delta of a single commit rebased on top of another. That is:

正如威廉所建议的那样,挑选樱桃可以为您提供基于另一个提交的单个提交的增量。那是:

$ git checkout 012345
$ git cherry-pick -n abcdef
$ git diff --cached

This takes commit 'abcdef', compares it to its immediate ancestor, then applies that differenceon top of '012345'. This new difference is then shown - the only change is the context comes from '012345' rather than 'abcdef's immediate ancestor. Of course, you may get conflicts and etc, so it's not a very useful process in most cases.

这需要提交'abcdef',将其与其直接祖先进行比较,然后将该差异应用于'012345'之上。然后显示了这个新差异 - 唯一的变化是上下文来自 '012345' 而不是 'abcdef 的直接祖先。当然,您可能会遇到冲突等问题,因此在大多数情况下这不是一个非常有用的过程。

If you're just interested in abcdef itself, you can do:

如果你只是对 abcdef 本身感兴趣,你可以这样做:

$ git log -u -1 abcdef

This compares abcdef to its immediate ancestor, alone, and is usually what you want.

这将 abcdef 与其直接祖先单独进行比较,并且通常是您想要的。

And of course

而且当然

$ git diff 012345..abcdef

gives you all differences between those two commits.

为您提供这两次提交之间的所有差异。

It would help to get a better idea of what you're trying to achieve - as I mentioned, asking for the difference between two commits without what's in between doesn't actually make sense.

这将有助于更好地了解您要实现的目标 - 正如我所提到的,要求两次提交之间的差异而没有两者之间的差异实际上没有意义。

回答by plexoos

To compare two git commits 12345 and abcdef as patches one can use the diff command as

要比较两个 git commits 12345 和 abcdef 作为补丁,可以使用 diff 命令作为

diff <(git show 123456) <(git show abcdef)

回答by roadev

git diff <a-commit> <another-commit> path

Example:

例子:

git diff commit1 commit2 config/routes.rb

It shows the difference on that file between those commits.

它显示了这些提交之间该文件的差异。

回答by bit_cracker007

For checking complete changes:

要检查完整的更改:

  git diff <commit_Id_1> <commit_Id_2>

For checking only the changed/added/deleted files:

仅检查更改/添加/删除的文件:

  git diff <commit_Id_1> <commit_Id_2> --name-only

NOTE: For checking diff without commit in between, you don't need to put the commit ids.

注意:为了检查差异而不在两者之间提交,您不需要放置提交 ID。

回答by Juanpa

Let's say you have this

假设你有这个

A
|
B    A0
|    |
C    D
\   /
  |
 ...

And you want to make sure that Ais the same as A0.

并且您想确保它AA0.

This will do the trick:

这将解决问题:

$ git diff B A > B-A.diff
$ git diff D A0 > D-A0.diff
$ diff B-A.diff D-A0.diff

回答by William Pursell

Suppose you want to see the difference between commits 012345 and abcdef. The following should do what you want:

假设您想查看提交 012345 和 abcdef 之间的区别。以下应该做你想做的:

$ git checkout 012345
$ git cherry-pick -n abcdef
$ git diff --cached

回答by Flow Overstack

What about this:

那这个呢:

git diff abcdef 123456 | less

It's handy to just pipe it to less if you want to compare many different diffs on the fly.

如果您想即时比较许多不同的差异,只需将其减少即可很方便。

回答by Tomá? Divi?

Since Git 2.19, you can simply use:

从 Git 2.19 开始,您可以简单地使用:

git range-diff rev1...rev2- compare two commit trees, starting by their common ancestor

git range-diff rev1...rev2- 比较两个提交树,从它们的共同祖先开始

or git range-diff rev1~..rev1 rev2~..rev2- compare of changes introduced by 2 given commits

git range-diff rev1~..rev1 rev2~..rev2- 比较由 2 个给定提交引入的更改

回答by Jinmiao Luo

My aliassettings in ~/.bashrcfile for git diff:

alias~/.bashrc文件中的设置git diff

alias gdca='git diff --cached' # diff between your staged file and the last commit
alias gdcc='git diff HEAD{,^}' # diff between your latest two commits