Kamal Fariz Mahyuddin on Ruby on Rails, Ember.js and other web development geekery.

You should follow me on twitter here.

Git Utilities You Can’t Live Without

I found some new git toys to add to my toolbox. Let me share them with you:

1. git-completion.bash

The git tarball contains a sweet bash completion routine for git. Stick a copy from git- to /opt/local/etc/bash_completion.d/git-completion and tab away!


MacBook-Pro:webrat kamal$ git <tab><tab>
add                 cherry              diff                instaweb            rebase              show-ref
am                  cherry-pick         fast-export         log                 relink              st
annotate            ci                  fetch               lost-found          remote              stash
apply               citool              filter-branch       ls-files            repack              status
archive             clean               format-patch        ls-remote           request-pull        submodule
bisect              clone               fsck                ls-tree             reset               svnimport
blame               co                  gc                  merge               revert              tag
br                  commit              get-tar-commit-id   mergetool           rm                  up
branch              config              grep                mv                  send-email          var
bundle              convert-objects     gui                 name-rev            shortlog            verify-pack
checkout            count-objects       imap-send           pull                show                whatchanged
checkout-index      describe            init                push                show-branch
MacBook-Pro:webrat kamal$ git br<tab>
br       branch
MacBook-Pro:webrat kamal$ git branch <tab><tab>
HEAD            master          onchange        origin/HEAD     origin/master

Pastie link if you can’t see it in full.

I like that last bit the most. It found my available branches. It can do a whole lot more. From the inline README:

- local and remote branch names
- local and remote tag names
- .git/remotes file names
- git 'subcommands'
- tree paths within 'ref:path/to/file' expressions
- common --long-options

2. Git-aware PS1

Once you have the git-completion file, you can set your PS1 to display the current branch you are in.

For stock-looking Leopard PS1, stick this in your ~/.bash_profile

PS1='\h:\W$(__git_ps1 "(%s)") \u\$ '


MacBook-Pro:src kamal$ cd webrat/
MacBook-Pro:webrat(master) kamal$ git co onchange
Switched to branch "onchange"
MacBook-Pro:webrat(onchange) kamal$

It’ll only display the branch if it senses you are in a git repo. Freaking sweet or what??

3. git.rake

I stole this Rake task from the Rubinius project. Stick it in your lib/tasks directory. Rake now has new git powers.

MacBook-Pro:webrat(master) kamal$ rake -T git
(in /Users/kamal/src/webrat)
rake git:push    # Push all changes to the repository
rake git:status  # Show the current status of the checkout
rake git:topic   # Create a new topic branch
rake git:update  # Pull new commits from the repository

Now everytime you want to work on something, create a git topic branch by issuing rake git:topic.

MacBook-Pro:webrat(master) kamal$ rake git:topic
(in /Users/kamal/src/webrat)
Topic name (default quick): feature_x
git checkout -b feature_x
Switched to a new branch "feature_x"
MacBook-Pro:webrat(feature_x) kamal$

Edit, edit, edit. Commit. Time to push upstream with rake git:push.

MacBook-Pro:webrat(feature_x) kamal$ rake git:push
(in /Users/kamal/src/webrat)
Switched to branch "master"
* Switching back to master...
* Pulling in new commits...
git fetch
git rebase origin
Current branch master is up to date.
* Porting changes into feature_x...
Switched to branch "feature_x"
git rebase master
Current branch feature_x is up to date.
* Merging topic 'feature_x' back into master...
Switched to branch "master"
git merge feature_x
Updating e25c574..0eb83d5
Fast forward
 doc/README_FOR_APP |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
* Pushing changes...
git push
Counting objects: 7, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 414 bytes, done.
Total 4 (delta 1), reused 0 (delta 0)
refs/heads/master: e25c57 -> 0eb83d
   e25c574..0eb83d5  master -> master
* Switching back to feature_x...
Switched to branch "feature_x"
MacBook-Pro:webrat(feature_x) kamal$

Dude, seriously. All that switching around, fetching, rebasing, merging? I bet only Linus Torvalds remembers the whole sequence off the top of his head.

Hope you enjoyed that. Also, I’ve got 5 GitHub invites if anyone wants ‘em.

Fork me on GitHub