Git Tricks: git-summary

Mercurial has a useful command, hg summary, that prints out some basic information about your current repository status. git status prints most of this information, but omits some useful details such as the ID and message of the most recent commit.

The following shell script will fix this, printing the current commit followed by the output of git status:

#!/bin/sh

HEAD_REV=`git rev-parse --short HEAD 2>/dev/null`
if [ "$?" -ne 0 ]; then
    exec git status
fi
HEAD_MSG=`git show -s --format=%s HEAD`

echo "Parent revision is $HEAD_REV: $HEAD_MSG"
exec git status

Save this script somewhere on your $PATH as git-summary (~/bin is a good option). If you are on Windows, save it in a file called git-summary in the usr\bin subdirectory of your Git installation (often C:\Program Files\Git). Then you can run git summary to see this augmented display:

Parent revision is 0ccf9db: Support default path
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
nothing to commit, working directory clean

There’s one more trick I use to make this even more useful. In my Mac, Linux, and BSD environments, I have the following in my ~/.profile:

git()
{
    if [ -z "$1" ]; then
        command git summary
    else
        command git "$@"
    fi
}

This function redefines the git command so that running git without any arguments runs git summary. The command shell built-in tells the shell to run the real git command even though there is a shell function with the same name.

With the git-summary script and this shell function, I can just type git and it will give my my bearings in the current repository.