Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to list commits on this branch but not from merged branches

Suppose your git commit history looks like this:

A---B---C---D---E---F master      \         /       X---Y---Z topic 

Is it possible to have git list only the commits on master, A-F? In other words, if the commit was on a merged-in branch, I don't want it show.

like image 407
wch Avatar asked Apr 20 '12 14:04

wch


People also ask

How can I see commits from one branch?

Git rev-list will list commits in one branch that are not in another branch. It is a great tool when you're trying to figure out if code has been merged into a branch or not. Using the --oneline option will display the title of each commit.

How do I find commits between two branches?

In order to see the commit differences between two branches, use the “git log” command and specify the branches that you want to compare. Note that this command won't show you the actual file differences between the two branches but only the commits.

How do I check my master commits?

To confirm, you can run git branch . The branch that you are on will be the one with a * next to it. Git checkout might fail with an error message, e.g. if it would overwrite modified files. Git branch should show you the current branch and git log master allows you to view commit logs without changing the branch.


2 Answers

git log has option --first-parent, so you won't get topic history.

When merged from master, the master commits are the first parents in merge. Git log allows to display only those commits with --first-parent, so you get the right stuff.

like image 80
CharlesB Avatar answered Oct 05 '22 15:10

CharlesB


TLDR : git log origin/master --no-merges will give you a log of master and exclude any merged commits ( in this case x, y, z )

Original Points

There is another general way to go about this that doesn't rely on --first-parent which will be helpful in certain situations.. using the branch exclusion filters

git log origin/topic ^origin/master This will give you a log of origin/topic with all of origin/master's commits removed.

you could also add in --no-merges which will hide merge commits which you may or may not want.

Another handy tip is to use shortlog instead of log which will give you more of an abbreivated summary that can be handy for release notes, or communication of whats in a branch.

Update
After re-reading this, you actually would want nearly the inverse of what I posted; however it would end up excluding everything that is on master and foo ( git log origin/master ^origin/foo ) . However you could also get what you ask for ( hide all commits that are part of merges) with git log origin/master --no-merges

like image 32
UpAndAdam Avatar answered Oct 05 '22 14:10

UpAndAdam