Showing the Count of Commits in git log

Why and how to level up your rebase with numbered commits. Like this:


Why?

I rebase. A lot. While prepping for a presentation I rebased 40-some atomic commits down into 6 logical commits. A typical rebase workflow looks something like this:

git log

Count back a bunch of commits. Looks like the one I want is 5 commits back.

git rebase -i head~5

Reorder some commits, squash a few, edit some messages.

Repeat.

With 40+ commits in play during a rebase, counting backwards becomes a tedious proposition. I wanted a quick way to get that number.

The Code

Add the following git alias:

# ~/.gitconfig
[alias]
  ln = log --pretty=format:'%Cblue%h %Cred* %C(yellow)%s'

And the following bash alias:

# ~/.bashrc.local
alias gln="git ln | perl -ple 's/\*/sprintf(\"%2s\", \$n++)/e' | less"

Then just source ~/.bashrc.local and run gln in any git repository. Enjoy!