gitgithub

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


Solution

  • 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