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.

Request a Call

Looking for support?

Send Us Your Feedback

Provide us with some information about yourself and we'll be in touch soon. * Required Field