Improving my git workflow

trevor

Posted by trevor at November 10th, 2008

I'm a fan of "rip and tear" refactoring. Dive in, hack and slash, see what works. Whether my hack-and-slash-fest lasts a few minutes or a few hours, I always do my dirty work in a git 'topic branch'.

A topic branch is simply:

git checkout -b trevor-287-date-formatting
# hack and slash

NB: at ENTP we name our branches according to committer, Lighthouse ticket #, and a short descriptive string.

At every step along the way, when I think "okay, that works", I commit. That way, as I continue on my path of destruction, any dead ends I encounter can be undone with a simple "git checkout -- ." - a project-sized undo buffer, if you will.

Sometimes I'll even branch off of my topic branch to try out ideas. Cheap to create, cheap to destroy.

So topic branches and frequent commits are my safety net. They let me be as aggressive as I want in my changes and help me to maintain 'flow'. Except...

Except for when it comes time to publish my topic branch for peer review. I may be a hack-and-slash bandit but at ENTP we've got some pretty stringent requirements for making sure code "makes the grade" before it even hits the staging servers, let alone production.

That means that every change gets pushed to a remote branch and reviewed by at least one other brainiac as a first step. And this is where my lovely topic branch has let me down, just a little.

Pushing a branch to remote is easy:

git push origin trevor-287-date-formatting

The only problem is .git/config isn't updated so that I can do a simple "git pull" on those rare occasions Giles or Matt would have the audacity to correct my finely-crafted code. Until now I've been hand-editing .git/config to make sure everything works. Blech.

What I've wanted is a way, with a flick of the keyboard, to 'promote' my topic branch of trevor-287-date-formatting to a tracked remote branch of the same name.

And while I could easily start the whole process with:

# create the new remote branch based on current master:
git push origin master:refs/heads/trevor-287-date-formatting
# create a local tracking branch of trevor-287-date-formatting
git branch --track trevor-287-date-formatting origin/trevor-287-date-formatting
# start working on it:
git checkout trevor-287-date-formatting

It just doesn't feel fluid enough to me. Remember, topic branches appear and disappear from my local repo faster than stock value on the Dow Jones.

Instead, I wrote a shell script that encapsulates my manual steps (push to remote, amend the config) into a concise command. Much more satisfying.

I present to you: git-promote

#!/bin/sh
#
# Promotes a local topic branch to a remote tracking branch of the same name,
# by pushing and then setting up the git config

curr_branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')

remote_branch=$(git branch -r | grep "origin/${curr_branch}")
[ -z "${remote_branch}" ] && ( git push origin "${curr_branch}" )

origin=$(git config --get "branch.${curr_branch}.remote")
[ -z "${origin}" ] && ( git config --add "branch.${curr_branch}.remote" origin )

merge=$(git config --get "branch.${curr_branch}.merge")
[ -z "${merge}" ] && ( git config --add "branch.${curr_branch}.merge" "refs/heads/${curr_branch}" )

Assuming you have $HOME/bin in your PATH, put that script in a file called $HOME/bin/git-promote and type this on the command line:

chmod 755 $HOME/bin/git-promote

Now you can promote your topic branches to remote tracking branches like this:

# just to ensure sure my working branch is the one I want to promote...
git checkout trevor-287-date-formatting
git promote

Done and Done.

If anyone has better suggestions, do let me know. I'm always keen to learn about other people's workflows.

4 Comments

  1. Josh Knowles Josh Knowles said on November 11th, 2008

    You should checkout out git_remote_branch, http://github.com/webmat/git_remote_branch/tree/master, as it encapsulates a lot of what you’re doing and then some.

  2. Trevor Squires Trevor Squires said on November 12th, 2008

    Josh – cool! That does look pretty comprehensive, will check it out.

  3. Glenn Rempe Glenn Rempe said on November 12th, 2008

    You might also checkout (literally):

    git://repo.or.cz/git-walkthrough-commit.git

    I think the git-publish-branch tool found within also does what you are doing. And allows you to delete the remote branch and local config too with the -d flag.

  4. Garry Garry said on November 12th, 2008

    That’s cool, I’ll be adding your script to my git toolbelt, thanks.

Make your voice heard

Sorry, but comments are closed for this item.