A Web Developer's Field Guide

Dead Simple Git Workflow

Category
GIT
Last Update
09 October 2010
Tweet
Author

Recently I have had the privilege to work with a few excellent small agile teams. With each team, they have their own process, which works best for them. And there’s nothing wrong with that, a team should work in it’s most effective way. But I came across a team recently that had a workflow that really impressed me. To put it simply it’s described as an extremely cool Git workflow.

I can’t take credit for coming up with this workflow, but I’m gonna do my best to re-describe it here so it can be shared. The basis for this post comes from reinh.com’s posts A git workflow for Agile Teams and Hack && Ship. Also this workflow pulls alot from Josh Susser’s post on the story branch pattern.

Let’s start by summarizing Josh’s post. In his post he describes a 7 step process that begins with Pivotal Tracker. The idea being you locate a story on pivotal tracker, like a bug, or feature, or chore. Then you update your working master by pulling from the origin, and create a whole branch just for that bug. Once you’ve fixed the bug, commited it to your branch, you then merge your branch with the master and push it all back up to the origin. Then just remove the old branch and you’re done.

That’s the basic workflow, but there is alot of automation that can be done. The first is the creation of the branch. This isn’t terribly long command ‘git checkout -b’ but I went ahead and created a shell alias ‘gcb’.

1 alias gcb='git checkout -b'

So now when creating my branch for working on my bug I just type

1 gcb fix_ie_bug_Story123456

After creating my branch I would work on it normally doing ‘git commits’ when needed. Then when I’m ready comes the cool part. Hack & Ship. Hack is what it’s called when you pull the lastest code from the origin and merge it with your branch. It’s a good idea to do this often, just to keep everything up to date. To not have to type and remember the few commands for accomplishing this, I use a shell script.

hack.sh

1 #!/bin/sh -x
2 \# hack: Merge the latest changes from the master branch into your current branch
3 ref=$(git symbolic-ref HEAD 2> /dev/null) || exit 0
4 CURRENT="${ref#refs/heads/}"
5 git checkout master
6 git pull origin master
7 git checkout ${CURRENT}
8 git rebase master

So during my coding, I can at anytime just type the command hack. And my branch is updated!

Once I’ve finished my bug and I’m ready to send it back to the origin I do one more ‘hack’, make sure it’s all updated. And then I ship. This will checkout the master merge my branch with it, and then push it to the origin. Once again I use a shell script for this.

ship.sh

1 #!/bin/sh -x
2 \# Git workflow ship script from: http://reinh.com/blog/2008/08/27/hack-and-and-ship.html
3 \# git name-rev is fail
4 ref=$(git symbolic-ref HEAD 2> /dev/null) || exit 0
5 CURRENT="${ref#refs/heads/}"
6 git checkout master
7 git merge ${CURRENT}
8 git push origin master
9 git checkout ${CURRENT}

Now I’m done with my bug, and there’s a script for that ‘dwf’. This will move me back to the master, and delete my old branch.

dwf.sh

1 #!/bin/sh -x
2 \# dwf, aka "Done With Feature" script: deletes current branch and puts you back on master
3 ref=$(git symbolic-ref HEAD 2> /dev/null) || exit 0
4 CURRENT="${ref#refs/heads/}"
5 git checkout master
6 git branch -d ${CURRENT}

And we’re ready to repeat the whole process on a new bug/feature/chore.

You can even take this a bit farther, and create an alias for the last three commands ‘hsd’ (hack ship done)

1 alias hsd="hack && ship && dwf"

Pretty awesome! I’m sure there are hundreds of awesome workflows out there. What do you guys use on your projects?