markmcdermott.io (js, ruby, ...etc) by mark mcdermott

Where's Your Head At?

Number of Commits Ahead/Behind

05/21/2025

read time:2 mins

Another day, another realization that I still have a lot to learn in Git. Today’s medium-dive: understanding how changes on a development branch compare to main.

Precondition

Local Branch Vs Tracking Branch

Changes Vs Local Master

Number of Commits Ahead/Behind Local Master

git rev-list --left-right --count master...HEAD

Visual of the Commits Ahead/Behind Local Master

Number of Commits Ahead/Behind Local Master in Plain English

git rev-list --left-right --count master...HEAD | awk '{print "Behind by "$1" commits, ahead by "$2" commits."}'

Changes Vs Remote Master

Number of Commits Ahead/Behind Remote Master

git rev-list --left-right --count origin/master...HEAD

Visual of the Commits Ahead/Behind Remote Master

Number of Commits Ahead/Behind Remote Master In Plain English

git fetch origin && git rev-list --left-right --count origin/master...HEAD | awk '{print "Behind by "$1" commits, ahead by "$2" commits."}'

Misc Helpful stuff

git branch -vv
git checkout master
git fetch origin
git pull --ff-only   # or: git reset --hard origin/master

Above Commands Using Main

And for easy copy/pasting if you use main instead of master: