96

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.

3 Answers 3

137

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

Sign up to request clarification or add additional context in comments.

3 Comments

This first command is excellent in "proper branch-per-feature" (plus.google.com/109096274754593704906/posts/R4qkeyRadLR)
It seems as though I get mostly commits from that branch, but it's still showing commits that were pulled from the master. We have so many branches that excluding others would be impractical.
@ChannelCat why not rebase your branch onto the other, then all the commits will be at the end?
8

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"

2 Comments

As my try to edit your answer got rejected, I'll post it here: in your last git line, there should be three dots, not two, between names of branches. Like this: your-branch...master
@vedranm You don't need 3 dots, only two...although 3 works in the same way that 2 works.
4

A very hackish way:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.