git commit count

I have a tendency of creating many small commits when working on a new feature.

While this may have some advantages, like ease of review, clear context and quick fallbacks, I find myself in need of squashing these commits before the actual merge.

In order to use git’s interactive rebase to rearrange my commits[1], I need to find out the number of relevant commits.

Sure, I can use tig or git log --pretty=oneline and simply count them, but where’s the fun in that?

Count commits

Today I learned how to count commits between different branches AWA within a branch.

# of commits between master and the current branch:

$ git rev-list --count master..HEAD
  • You can replace the master and HEAD parameters with any branch name or commit SHA

  • The command uses rev-list flag which lists commit objects in reverse chronological order, then counts them from the starting index to the final one.

[1] $ git rebase -i HEAD~<number-of-commits>

comments powered by Disqus