Shaun Luttin
Shaun Luttin

Reputation: 141792

Git log graph, display how two branches are diverging

We would like to view a graph of how two branches are diverging. Running git log --oneline --graph displays only the current branch. How do we include both branches in the graph?

Upvotes: 72

Views: 27891

Answers (4)

fikr4n
fikr4n

Reputation: 3430

I had the same issue and landed here, but no answer helped me to display how two branches are diverging. Eventually I did experiment myself and found this worked.

Given branch A and branch B, I want to see where they diverged.

git log --oneline --graph --decorate A B `git merge-base A B`^!

Note: Don't forget there is ^! at the end. (It excludes the parents of the commit returned by merge-base.)

UPDATE

The one line command above isn't working in case merge base is more than one. In this case do this:

mb_parents=$(git merge-base --all A B | sed 's/$/^/')
git log --oneline --graph --decorate A B --not $mb_parents

Upvotes: 44

Mark McDonald
Mark McDonald

Reputation: 8210

There's a newer option now, and while it's not quite as comprehensive as @fikr4n's answer, it gives similar information.

git log --graph currentbranch...otherbranch

From the man page:

Another special notation is "<commit1>...<commit2>" which is useful for merges. The 
resulting set of commits is the symmetric difference between the two operands. The
following two commands are equivalent:

     $ git log A B --not $(git merge-base --all A B)
     $ git log A...B

Using --graph helps because it will show the two branches as two disconnected graphs. Without it, it's not as clear which branch the commits belong to.

Upvotes: 0

chepner
chepner

Reputation: 532538

git log takes zero or more commits as arguments, showing the history leading up to that commit. When no argument is given, HEAD is assumed. For your case, you want to supply the two branch heads you want to compare:

git log --graph --oneline currentbranch otherbranch

If it doesn't display too much, you can simplify this by using

git log --graph --oneline --all

which acts as if you had specified every reference in .git/refs as the commits to display.

Upvotes: 90

Konrad Szałwiński
Konrad Szałwiński

Reputation: 400

git log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

Upvotes: -2

Related Questions