Tue 15 Sep 2009
Git – Working on a User Story
We recently started using Git as our version control system at Rally. Switching to Git has been a learning experience for many of us. I would like to share our experiences using Git in a team environment.
Working on a user story.
It’s Monday morning of a new iteration, we need to start the first user story of the iteration. The first thing we want to do is create a topic branch. In Git, we always want to work on a topic branch. We name our topic branch after our current story.
git checkout -b enhanced_search
After executing the above command, our repository will look like the following.
(A) -- (B) -- (C)
|
master
|
*enhanced_search*
Note: The current branch is surrounded by asterisks.
During the course of completing the story, we make two commits.
(A) -- (B) -- (C) -- (D) -- (E)
| |
master *enhanced_search*
We now want to integrate our commits into master. What do we do? Git has two options for integrating our commits; merge or rebase.
Merge
The first thing we need to do is a fetch. A fetch ensures we have a copy of the currently pushed commits.
git fetch
Someone has pushed a commit since we started working on the story.
origin/master
|
+---------(F)
/
(A) -- (B) -- (C) -- (D) -- (E)
| |
master *enhanced_search*
We now need to go back to master and make sure it is updated with the latest commits.
git checkout master git pull
origin/master
|
*master*
|
+---------(F)
/
(A) -- (B) -- (C) -- (D) -- (E)
|
enhanced_search
git merge enhanced_search
origin/master
|
| *master*
| |
+---------(F) -- (G)
/ /
(A) -- (B) -- (C) -- (D) -- (E)
|
enhanced_search
Our commits have now been merged into our local master branch. The last thing we need to do is push our commits to the origin.
git push origin master
origin/master
|
*master*
|
+---------(F) -- (G)
/ /
(A) -- (B) -- (C) -- (D) -- (E)
|
enhanced_search
Rebase
Git provides another option to merging called rebase. Rebasing in Git allows you to rewrite your commit history. This is a very powerful and potentially dangerous operation. When using rebase, always remember the golden rule.
Never rebase a branch that has been pushed.
Let’s start back at the beginning. We had made a couple of commits on the enhanced_search branch. While working on the enhanced_search someone on the team has pushed a new commit to the origin.
origin/master
|
+---------(F)
/
(A) -- (B) -- (C) -- (D) -- (E)
| |
master *enhanced_search*
We are now ready to rebase our enhanced_search branch with respect to origin/master. Keep in mind, our above picture shows our local repository after performing a fetch. Always perform a fetch before starting any merge or rebase operation.
git rebase origin/master
origin/master
|
(A) -- (B) -- (C) -- (F) -- (D') -- (E')
| |
master *enhanced_search*
Our commits from the enhanced_search have been replayed on the current origin/master. We are now ready to push our changes to the origin.
git push origin HEAD:master
origin/master
|
(A) -- (B) -- (C) -- (F) -- (D') -- (E')
| |
master *enhanced_search*
Should we Merge or Rebase?
I like the rebase approach. Why? I think the commit history is easier to understand. The reader of the history doesn’t know or care that we used a local topic branch. In the end, both approaches allow us to integrate our commits.
Stay tuned for more of our experiences with Git.

Excellent concise description. I’d like to hear about how topic branches fit in with your continuous integration strategy.
Social comments and analytics for this post…
This post was mentioned on Twitter by adamesterline: Just Blogged about Git in a team environment – http://bit.ly/17jmb7 #git…
[...] Git – Working on a User Story (11) [...]
@Abbot of Unreason
We really only use topic branches on the local machines. The topic branches never get seen by continuous integration.
[...] Git – Working on a User Story » Engineering Blog [...]
We started out using topic branches and rebasing as well, but later switched
to simple merging.
The main reason we did this was collaboration. If two people are working on the same topic branch and merge their changes together, when you rebase that
against master you’re likely to run into issues.
When the two developers work in parallel and then merge the commits. The
commits are applied from from one developer to another, conflicts are
resolved and then work is continued.
When you rebase that branch, git rebase reapplies the commits to master in
chronological order, which may be different from the order they were merged.
This can result in conflicts having to be resolved a second time or new and
more difficult to resolve conflicts.
You could probably just have the two developers always rebase their work with
each other, but all it takes is someone accidently merging to to cause
issues. Not to mention rewriting history is always a dangerous approach to
revision control and one person is going to have to delete and re-pull their
branch after one person performs the rebase.
In the end, we decided it wasn’t worth the headache just to get a pretty tree.
git log and gitk seem to work fine to figure out the history quickly and
we’ve run into considerably fewer problems when merging releases.
Nice article! Good to see we’re not the only ones who have gone through this
workflow dilemma!