Metaphysical Developer

Navigating Git Branch History

Posted in Uncategorized by Daniel Ribeiro on October 19, 2012

In last years I’ve been collaborating in very large projects using git. More recently, one thing that started annoying me was branch switching, as git shell completion started requiring a lot of cycling over similar named branches. Worse: most of the time I just needed to cycle among a few branches at a time (usually one branch I’m fixing, master for quick experiments on what is in production, and finally my working feature branch).

Git reflog exposes out a quick way to do this, using the shorthand HEAD@{0}, HEAD@{1}. Unfortunately, this is a bit too fine grained, as commits are recorded into the reflog.

After learning a bit about git’s hooks, I wrote a simple utility called git-nav to help me. A sample usage (just put the git-nav script in your path to install):

$ git nav
Creating post-checkout hook...
Already on 'master'
$ git commit c1
$ git checkout -b cool-feature
$ git commit c2
$ git nav l
Navigation History: (use 'git nav [number]' to go to that point )

1        master
$ git nav
Switched to branch 'master'
$ git commit c3
$ git checkout merge cool-feature
$ git commit c4
$ git checkout 4e6bc147ee9074998ac0406c1a646d5d3bbeaced
$ git checkout -b quick-fix
$ git-nav
Switched to branch 'master'
$ git nav l
Navigation History: (use 'git nav [number]' to go to that point )

4        master
3        cool-feature
2        master
1        4e6bc147ee9074998ac0406c1a646d5d3bbeaced

$ git nav 3
Switched to branch 'cool-feature'

So, it is pretty simple:

git-nav l : Lists your navigation history.

git-nav [number] : Goes back to your navigation history by number steps.

git-nav : Goes back to last branch. Same as git-nav 1.

If you think it may help you as well, you can grab it here.