Bash script to open GITK with local and remote branch

EDIT(2015-12-03): Updated the script with the current version i use.. think I should put this on github instead :)

So, gitk is a nice tool to utilize when you want to get a nice graphical view of your git repository or when you wonder ‘where the heck am I in my commit graph’.

As arguments to ‘gitk’ you can specify the branches you want ‘gitk’ to show or use the default which is ‘HEAD’. You can also specify ‘–all’ which shows you all your branches, which can be a bit much.

Something that I often want to do is view a branch (or the current) AND it’s remote branch, which often is ‘master’ and ‘origin/master’ but that is not always the case.

Below is a script I wrote that will take one branch as argument, or default to the current branch and lookup it’s remote branch (checking your git config) and after that it opens ‘gitk’.

I usually put’s this in a file called ‘gk.sh’ which I have in ‘~/bin/’ (which is in my PATH).

#!/bin/bash

function git_namerev {
	git name-rev --name-only $1
}

BName="$1"
if [ -z "$BName" ]; then
  BName="HEAD"
fi

BRANCH="$(git_namerev $BName)"
if [ "$BRANCH" = "undefined" ] ; then
  BRANCH="$BName"
fi


REMOTE_BRANCH=""
TRACKING_REMOTE=`git config branch.$BRANCH.remote`
if [ $? -eq 0 ]; then
  TRACKING_BRANCH=`git config branch.$BRANCH.merge`
  TRACKING_REMOTE=`git config branch.$BRANCH.remote | cut -d "/" -f 3`
  if [ $? -ne 0 ]; then
	echo "No specific remote branch tracked, trying to use local branch name"
	TRACKING_REMOTE=$BRANCH
  fi

  REMOTE_BRANCH="$TRACKING_REMOTE/$BRANCH"
  BLIST=$(git branch -a | grep remotes/$REMOTE_BRANCH)
  if [ -z "$BLIST" ]; then
	echo "Remote branch not found! '$REMOTE_BRANCH'"
	exit 1
  fi

fi

shift
#echo gitk $BRANCH $REMOTE_BRANCH
gitk $BRANCH $REMOTE_BRANCH $@