The Perfect Git Workflow for a One Person Project

A few months ago, I started investigating Git, and I fell in love with how much easier it made managing my code. I’m managing the source code to this site with Git, and along the way I’ve come up with a pretty good workflow for myself. The basic steps are:

  1. Branch
  2. Commit
  3. Rebase
  4. Merge
  5. Deploy

Let’s go through each one to see how it all works together.

Branch

With SVN, I found myself hating branching–it was always a complicated procedure, and I could never remember how to do it. In Git it’s as easy as git checkout -b <branch-name>, and you’re ready to go. Once you have a branch, you can modify it however you want, and you don’t have to worry about interfering with the master branch. In order to keep your master branch bug free, commit only to branches, not to master itself.

In addition to creating new branches for major features, I always have a few branches around that I pop into for certain things:

Having these branches allows me to make small fixes to the site, and if it turns out it’s more than just a small fix, it doesn’t interfere with anything else.

Commit

In the Subversion world, it’s a pretty common practice to make very large commits, consisting of many changes. With Git, you should constantly be committing. By making many commits, you make it easier to find bugs you may have introduced, and it makes it a lot easier to track your progress. If you don’t like the thought of wading through long lists of commits in your logs, don’t worry–before bringing it over to the master branch, you can consolidate things with interactive rebasing, but while you’re hacking away on a branch, it really is advantageous to have many small commits.

Rebase

Rebasing is one of the harder things to grasp when you’re first learning about Git. For in-depth coverage of the topic, check out the Rebasing page in the Git Community Book. In a nutshell, doing git rebase master takes any commits to master and inserts them into your current branch, so you can then make sure your new code still works, and it’s a lot less hazardous than doing a merge. Rebasing your branch before putting into master is really important because it allows you to deal with any merge issues before the code goes to your main branch. To rebase, just run git rebase master.

Merge

After rebasing the branch, it’s safe to merge it into master. Since I’ve already dealt with any merge issues with the previous step, it’s as simple as checking out the master branch and running git merge <branch-name>

Deploy

I use Capistrano to deploy my code, so I’m constantly typing git push followed by cap deploy to deploy changes to my server. To make it easier, I just put both commands into one git alias:

deploy = !git push && cap deploy

Now I just need to run git deploy and it automatically pushes all of my changes to the remote Git repository and then deploys the site using Capistrano. Here’s some more information about Git aliases.

Useful tools

Though I primarily use Git through the command line, I really like using GitX to visualize branches. To host my repositories on my server, I use Gitosis, though if I had a few more projects, I’d dole out the money for a paid account at GitHub.

Have a great Git workflow? Think mine’s terrible? Let me know in the comments!

Posted on July 30, 2009
Tagged with: , , , ,

5 Comments

Avatar

Mat Schaffer 27 Oct 2009 at 11:40PM

Nice overview! The one thing I’ll comment on is the rebase which can get messy. I advise just sticking to merges. If you accidentally rebase a branch that anyone else has a clone of, you’re in for some pain. The downside is a slightly more complicated history graph.

I’m hoping that the history visualization tools eventually mature to a point where rebase isn’t really needed at all.

Avatar

Kyle 27 Oct 2009 at 11:51PM

Mat, glad you enjoyed it!

About rebasing: since this workflow is intended for one person, you are unlikely to encounter any rebase conflicts. However, I definitely agree with you–when working on a branch with others, you should avoid rebasing. Really, rebasing should only be used on local, temporary branches, which only you on are working on. Anything else should be merged.

Avatar

Colin Devroe 28 Oct 2009 at 7:59AM

Mat forgot to read the title. HA! (kidding)

Avatar

Mat Schaffer 28 Oct 2009 at 10:15PM

@colin I sorta did, but then saw it and figured I’d pipe up anyway just to help point any beginners in the right direction.

Avatar

Bruno 29 Mar 2013 at 2:28AM

New git fun :)

Leave A Comment

Ajax-loader