Intro to Git and GitHub Part 3: Branching

This is a continuation of a tutorial blog series to learn the basics of Git, using GitHub and the GitHub client.

To start from the beginning, go here.

Now we start to touch on stuff that is a bit more complicated. There is a lot more you can do with branches, this tutorial will serve as an introduction to the concept.

What is branching?

When you make commits on git, you end up with a series of snapshots of the code in various states. What if you wanted keep the code in different states, without having to copy/paste into two different repositories?

For instance, you might have your 1.0 release version, and another 1.1 version for your in-progress new feature update.

What if while you are developing your 1.1 code, somebody finds a major bug that you need to fix from your 1.0 version right away? The new feature you are working on isn’t finished yet, but you don’t need to discard and revert all your hard work to get access to the 1.0 code. Instead, set up a new branch for your release and in-progress code.

What if you have a master version of your code, and you just want to mess around and change things just for testing? You want to be able to commit your work and save what you’ve been doing, but don’t want to mess up your master. You should not afraid to commit test changes because you would then have to revert them back later. Just make a new test branch instead that will not effect your master branch.

Branching allows you to organize your commits and keep track of multiple versions of your code.

This may sound weird at first, but it gets simpler once you see it in action.

Create a branch

By default, all git projects have a “master” branch. There is a drop down box that says “master” on top of your commit log. Click it to expand the drop down menu to view all your current branches.

Branching01To see more options, click the manage button on the right of the branches drop down.

Branching02Since we only have one branch, not much is going on in this screen right now.

To make a new branch, go back to your main project screen. The text box should say “Filter or create new”. Type the name of the new branch you want.

Branching03Now you have two branches. “master”, and “feature1″!

Switch between branches

To switch between, click on the drop down menu again.

Branching04Try selecting “feature1″ and make a new commit.

Branching05Now switch back to “master” branch. You’ll see that your new commit disappears. If you check your folder, the files are automatically changed. When you’re ready to work on your new feature again, select the branch again and you can continue working.

Deleting branch

To delete a branch, you’ll need to go to your branch manager again. To get there, click the branches drop down menu again and hit the manage button on the top right.

Branching06You can’t delete the current branch you have “checked out” (the one you have open). Other branches appear below, simply click the trash can to delete them.

End of Part 3

Click here to return to the main blog post. Part 4 will be posted on Friday.

-Tobiah

3 thoughts on “Intro to Git and GitHub Part 3: Branching

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>