Show non-merge differences for two commits in git

血红的双手。 提交于 2019-11-27 02:42:36

问题


I have two commits, once of which is the ancestor of another. (They happen to be the start and end points of a branch. I don't think that matters, but I'll include it if it does).

I want to see the diff between the two commits, but excluding changes made during merge commits (that is, all commits with more than one parent) that were made between the two commits. (Basically, I want any "real" commit that was made to the branch, excluding the merges.)

Is this possible? If so, how do you accomplish this?

If necessary, assume that there are no conflicts resolved during the merge commits... but bonus points for a solution that can handle them elegantly.


回答1:


Your question is slightly ambiguous but I think you want this.

git log --no-merges -p branch-start..branch-end



回答2:


If your merges all came from the same branch (say master) or are all contained in another branch, you can use the solution from this question.

Assuming you have a tree like the following:

         x---y-+-z-+-branch
        /     /   /
---a---b---c-+-d-+-e---master

and the two commits you would like to compare are b and branch. Then instead of comparing the two commits directly, running

git diff master...branch

will show all changes done on the branch (x,y,z) excluding everything that is also on master (c,d,e, the merges). Note that this also ignores any changes done on master that are not yet in the branch.

From the docs:

git diff [--options] commit...commit [--] […​]

This form is to view the changes on the branch containing and up to the second commit, starting at a common ancestor of both commits. "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B".




回答3:


I didn't know the --no-merges -o options but here another solution (I suppose that merges have been done from master) :

git checkout -b temp
git rebase --onto master branch-start branch-end
git diff master


来源:https://stackoverflow.com/questions/4549157/show-non-merge-differences-for-two-commits-in-git

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!