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

When using git, is there a way to show commits made to a branch, while ignoring all commits that were brought in by merging?

I'm trying to review the code changes made on a branch while ignoring the ones we made on other branches that were merged in. I know it's damn near impossible to show a diff in that fashion, but I'd like to be able to find out which commits I need to review.


Solution 1:

--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.

Solution 2:

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"

Solution 3:

A very hackish way:

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