---
title: "Improving git log"
slug: improving-git-log
section: tech
date: 2016-03-03T11:17:00.000Z
canonical: https://roland.leth.ro/blog/tech/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:

```bash
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.
3. 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](/tech/blog/improving-the-pull-request-from-terminal).
