Intro to Git-3: Branches

This is part 3 of my introduction to Git series. If you are unfamiliar with Git I would recommend looking at the other two tutorials.

Intro to Git-1: Basics

Intro to Git-2: Remotes

Branches allow our repositories to be a little more complex. One of the key feature of branches is to keep our additions to a project separate.

You might have notice the word “master” thrown around in the git commands we have been doing. Well, it turns out that we have been working on mater the whole time! Every git project works on some sort of branch.

A branch is a copy of your work that you can use to add new features.

Let me give an example of how git branches are useful. Lets say that you are working on a website and have a git repository for it’s code. Also, lets say that the code on the live deployed site is the exact same code as our master branch. If we are constantly working on master branch adding new experimental features, then what happens if we need to make a small change to the live site’s code that needs to be pushed right away? Well, we added all sorts of extra stuff on our master branch that we don’t want to put on the live site yet, so it would be really hard to add that small change while disregarding the new additions! However, if we keep master branch the same as the code in the live site, and add all of our new features in a separate branch, then making a small change to the live code is no problem because I can just switch back to the master branch, make the change, put the change on the live site, then switch back to my feature branch to continue developing!

Creating a new Branch

Remember that the default branch we are working on it “master”. You can verify this by typing in the command “git branch”. This will show you the current branch that you are on and any others that you might have. It should show you that you are on master if you haven’t created any new branches before.

My master has 3 commits on it.

Screen Shot 2016-10-01 at 10.14.31 AM.png

To create a new branch type “git branch <branchName>” where <branchName> is the name of the new branch.

Screen Shot 2016-10-01 at 10.11.14 AM.png

I created a new branch called featureBranch. Note that creating a new branch doesn’t switch you to that branch, you have to switch to it after creating it.

Type “git checkout <branchName>” to switch to the branch <branchName>.

Screen Shot 2016-10-01 at 10.15.30 AM.png

Notice that when I created that new branch, it has all of the commits from the branch that I created it from. This is useful because we need those in order to build off of existing content in our feature branch.

I added one more commit on feature branch.

Screen Shot 2016-10-01 at 10.18.33 AM.png

At this moment, that new commit I created is not on master, it is only on feature branch. This gives us some safety because any new commits that we are adding on feature branch should have to do with that particular feature and not be imported into master yet since we are maintaining master to be the same as the code from the live site.

Merging Branches

Once you have decided that the feature you are working on is finished, you can merge the feature branch with master (the branch dedicated to the code on the live site).

This is pretty simple, just switch to master branch by typing “git checkout master”. Then type “git merge <branchName>”. This will merge <branchName> into the branch that you are currently on, which in our case is master.

After I merged I should see that those new changes that I created on my feature branch are now on the master branch also. Note that this operation leaves our featureBranch unchanged, we are just copying the changes into another branch by using “git merge”.

Screen Shot 2016-10-01 at 10.25.08 AM.png

This is perfect! We now have a way to work on our features separate from master branch. That way we can dedicate master branch’s code to be correspondent to the code use on the deployed version!