Showing commits made directly to a branch, ignoring merges in Git

在使用 git 时,是否有一种方法可以显示对分支的提交,同时忽略通过合并引入的所有提交?

我试图检查对一个分支所做的代码更改,同时忽略我们对合并到其中的其他分支所做的代码更改。我知道以这种方式展示差异几乎是不可能的,但是我希望能够找到我需要审查的提交。

24562 次浏览

--no-merges

Both parents have equal weight in many contexts in git. If you've always been consistent in merging other changes in then you may find that this gives you what you want.

git log --no-merges --first-parent

Otherwise you may be able to exclude commits from other named branches.

git log --no-merges ^other-branch-1 ^other-branch-2 ^other-branch-3

If you want to review the changes that you are going to merge back into a principal branch then the easiest thing to do is to perform the merge on a local clone and then just look at the diff with the first parent before publishing the merge.

A very hackish way:

git log --graph --oneline --no-merges thebranch|grep '^\*'

You can use git cherry for that, it will find you commits that were not yet merged to the upstream, or commits that are on one branch but not the other. So given two branches named "your-branch" and "master":

git cherry -v your-branch master

will present you list of commits compared with their patch id:

+ c3e441bf4759d4aa698b4a413f1f03368206e82f Updated Readme
- 2a9b2f5ab1fdb9ee0a630e62ca7aebbebd77f9a7 Fixed formatting
+ e037c1d90b812af27dce6ed11d2db9454a6a74c2 Corrected spelling mistake

You can notice that commits prefixed by "-" are the ones that appear in both branches, whereas those prefixed with "+" are availble only on your branch.

As an alternative you can use:

git log --pretty=format:"%h %s" your-branch..master --no-merges

which will show you list of commits done on "your-branch" that are not yet present on "master"