如何让git diff只显示两次提交之间的差异,而不包括中间的其他提交?


当前回答

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

例子:

git diff commit1 commit2 config/routes.rb

它显示了两次提交之间在该文件上的差异。

其他回答

假设您想查看提交012345和abcdef之间的差异。以下内容可以达到你的目的:

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

直接在GitHub上查看差异;您可以- https://github.com/<username>/<reponame>/compare/<commit1>..<commit2> .

commit1和commit2可以是分支名或commit哈希值

为例:

比较gs/add-explicit-paths-to-js-files分支和gs/add-history-helper - https://github.com/twbs/bootstrap/compare/gs/add-explicit-paths-to-js-files..gs/add-history-helper 比较commit 75e09b1c0f5ae5f51078c7a25fe36d892c5cfcfe和585146a6a7aa70faf25442d7d28636ce57e29588 - https://github.com/twbs/bootstrap/compare/75e09b1c0f5ae5f51078c7a25fe36d892c5cfcfe..585146a6a7aa70faf25442d7d28636ce57e29588

在比较提交中阅读更多信息

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

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

或 Git range-diff rev1~..rev1 rev2 ~ . . rev2 -比较2个提交所带来的变化

为了比较12345和abcdef作为补丁的两个git提交,可以使用diff命令作为

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

美元git日志

commit-1(new/latest/recent commit)
commit-2
commit-3
commit-4
*
*
commit-n(first commit)

$git diff commit-2

显示commit-2到commit-1之间的所有变化(仅仅是commit-1的补丁,相当于git diff HEAD~1 HEAD)

类似的 $git diff commit-4 commit-1

显示commit-4到commit-1之间的所有更改(commit-1的补丁, 一起提交2号和3号。相当于git差头~3头)

$git diff commit-1 commit-2

通过改变订单提交ID,可以获得恢复补丁。 ("$git diff commit-1 commit-2 > revert_patch_of_commit-1.diff")