Mar 03, 2016:  ~ 2 min read

Improving git log

We can already use --graph and --decorate to get a pretty, colored commit tree, but we can create a function for less typing, more flexibility and more info:

git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}

# Short for git decorated log
gdl() {
  branches=''
  if [ -n "$1" ] && [ -n "$2" ]; then
    branches=$1..$2
  elif [ -n "$1" ]; then
    if [ $1 == "keyword" ]; then
      branches="$(git_branch)"
    else
      branches=$1.."$(git_branch)"
    fi
  else
    branches=master.."$(git_branch)"
  fi

  git log --oneline --graph --decorate $branches
  echo "${blue}Difference: ${green}$(git rev-list --count $branches) commits."
}

This will also print the number of commits between the two branches at the end, and the rules are as follows:

  1. If we pass in two parameters, it will show the differences between the two branches.
  2. If we pass in just one parameter it will either
  • print the current's branch tree, if the parameter is equal to the keyword (make sure it's something unique), or
  • show the differences between the current branch and the one passed in.
  1. If no parameter is passed in, it will show the differences between a fallback branch (in this case master) and the current branch.

The color helpers can be found here.

Subscribe to my monthly newsletter.
No spam, unsubscribe at any time.