Skip to content

Instantly share code, notes, and snippets.

@andycochrane
Last active January 6, 2020 08:47
Show Gist options
  • Save andycochrane/b182282af542fd3bb228d1be22049b38 to your computer and use it in GitHub Desktop.
Save andycochrane/b182282af542fd3bb228d1be22049b38 to your computer and use it in GitHub Desktop.
Compare 2 branches by number of commits behind/ahead metric
[alias]
ba = "!f() { \
LEFT=${1-master}; \
RIGHT=${2-HEAD}; \
OUTPUT=$(echo $(git rev-list --left-right --count $LEFT...$RIGHT) "$LEFT $RIGHT" | awk '{print $4\" is \"$1\" behind and \"$2\" ahead of \"$3}');\
echo $OUTPUT; \
}; f"
# Example:
# $ git ba develop feature/shiny-new-feature
# // feature/shiny-new-feature is 0 behind and 12 ahead of develop
# This git alias takes 2 branch names as arguments and will show how many
# commits branch 2 is behind and/or ahead branch 1.
#
# GitHub and Bitbucket have this feature but only compare branches with
# the default or main branch.
# This allows you to compare any 2 branches - including remote ones (don't forget to fetch first) - so you
# can make use of it even with your weird branching strategy.
#
# If no arguments are passed when using the alias, it will default to
# comparing HEAD with master (change the $LEFT variable to set an
# alternative main branch (e.g. develop)).
# Credit goes to this answer on stack overflow for the actual git command:
# https://stackoverflow.com/a/27940027
#
# I just turned it into an alias and formatted the output in a human-friendly way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment