comment 0

Getting Started with Git and Bitbucket – Part 2

The previous post explained how to do basic manipulations of a Git repo and have a copy of that repo hosted on Bitbucket. In this post, I’ll explain how to use Git to facilitate a feature branch workflow.

Creating a branch

Let’s say we have a file in our repo called colors.txt, and all it contains is English names for some colors. Our task is to put some RGB values with those colors.

git checkout -b add_rgb_to_colors

The above command (1) creates a new branch called add_rgb_to_colors and (2) switches to that branch. Now any changes and commits we make won’t affect the trunk (i.e., master branch) because we’re effectively working on a separate copy.

Just to make sure things are in the correct state, we should list the branches for this repo:

$ git branch
* add_rgb_to_colors
  master

The asterisk shows the active branch is add_rbg_to_colors, which is what we expected.

If you visit the Bitbucket page for the project, you can see what branches exist by clicking on the Branches icon:

bitbucket-branches

Switching branches

If you have multiple features being worked on at once, each of those features may have a branch associated with it. To change branches, use git checkout:

git checkout master
git checkout some_other_branch

Pushing the new branch

Now that we’ve made our changes on our separate branch, we need to push this back to the remote repo so that others can have access to it (e.g., for a code review). (Remember to make sure you’ve set the active branch to the one you want to push.)

git push -u origin add_rgb_to_colors

This tells Git that we want to push all our changes to the server and associate this local branch called add_rgb_to_colors with the branch about to be created on the server. The “-u” portion is only needed the first time the branch is created. (See the Resources section below for a link to a post that explains tracking branches.)

Merging and closing

At this point we have a branch with work that’s essentially finished, but it needs to be folded back into the rest of the project. Here are the four steps:

1. Make sure we’ve got the latest bits for all branches.

git pull origin
git checkout master

2. Merge the separate branch into the master branch.

git merge add_rgb_to_colors

3. Push the revised master branch up to the remote repo.

git push

4. Close the separate branch, because we’re done with it.

git push origin --delete add_rgb_to_colors

What’s next

Distributed version control is quite powerful when used effectively. I will admit there’s a learning curve for all the various commands and options for any version control system, not just for Git. There are plenty of tutorials, cheat-sheets, and documentation about Git that are only a search-engine query away. Of course, if you have suggestions for other topics you’d like me to describe, leave a comment below.

Resources

Leave a Reply

Your email address will not be published. Required fields are marked *