Benjamin
Benjamin

Reputation: 3656

How to list branches in git with the number of commits ahead or behind master?

I really like how the Github /branches page lists the branches and a column that indicates the number of commits ahead or behind master for a branch.

Is there a way to see this information in the console/cli?

Edit: This solution shows how to get the commits ahead/behind for a single branch. But how could I do that for all local branches?

enter image description here

Upvotes: 6

Views: 939

Answers (2)

Richard Jones
Richard Jones

Reputation: 1

I needed something like this as an alias, so converted it is:

alias gitbs='git for-each-ref refs/heads/ --format='\''%(refname:short)'\'' | while read branch; do echo -n "$branch: " ; git rev-list --left-right --count master..$branch ; done'

Upvotes: 0

phd
phd

Reputation: 94502

git for-each-ref refs/heads/ --format='%(refname:short)' |
while read branch; do
    echo -n "$branch: "
    git rev-list --left-right --count master..$branch
done

Docs:

https://git-scm.com/docs/git-for-each-ref

https://git-scm.com/docs/git-rev-list

Upvotes: 7

Related Questions