Git good… Branching for beginners

Steve Davis
Sep 7, 2018 · 4 min read

If you talk to anyone using Git, chances are you heard them speak about branches, master, merge, etc. If you are new to Git, it might overwhelm you a bit but trust me they sound more complicated than they are. I will try my best to explain branching in plain English.

Consider your favorite software or app. You would have spent considerable time using it and would have explored many of its features. You might also have an idea for a new feature for the product. You can suggest the product team build this new feature. Imagine that the product team likes your idea and even reach out to you and tell you to develop the feature. Highly unlikely, but it would be awesome if they did, wouldn’t it?

So how do you go about it? Once you have access to the source code, would you start editing it? No, you would want to have a copy of the code, develop the feature, see that it is working as intended, ensure that the new code does not break anything else in the product, refine your code, test it thoroughly and then include your code in the main code base to be deployed in production. Git branching helps you to do all of this easily, without any fear of messing up the original code or the product.

Many Version Control Systems(VCS) offer some variations of branching — ability to develop a new piece of code without affecting the main source code. But Git does it more efficiently than any other VCS because of the way Git stores data — using snapshots instead of files and changes made to the files separately. When you commit a change, Git stores a snapshot of the tree structure and the associated files. You can think of a branch as just a pointer to a specific commit. By default, the first pointer that references the first commit is called the “master” branch. Any further commits move the master pointer to reference the latest commit. To make our understanding easier, assume that the master branch is the code deployed on production.

When you want to start developing the new feature, you can create a separate branch on which you can make all your changes, without worrying about the master branch. When you create a branch, say “branch_1” from master, a new pointer is created pointing to the same commit that the master is pointing to, i.e., branch_1 is pointing to the code currently deployed on production. But before you make any changes, remember that you need to inform Git that you want to work on the code through branch_1. Git determines the branch you are currently working on using another pointer called “Head.” In our case, Head will now point to branch_1.

You can start writing your code in branch_1, and once you commit your new changes, a new commit is stored and only branch_1 points to the latest commit. Head would still point to branch_1, i.e., you are still working in branch_1. Note that master still points to the previous commit, which is on production. The changes you made have not affected the master and hence not the production environment.

If you think of a different way of implementing your feature, and if you want to start your development back from the old commit, you can just move the Head back to master and create a new branch, say “branch_2.” Move Head to “branch_2,” and you are all set to implement the feature differently. Once you commit the new changes, branch_2 points to your latest commit. Neither master nor branch_1 is affected.

Multiple branches created; branch_2 is the current branch

You can compare the code in two branches and decide which code you want to include in the product. Say that you decide branch_2 is better, and “merge” the branch with the master branch. The main source code now includes the code for the new feature. You are happy, the product team is happy, and the users of the product are happy. It’s a win-win-win! If you are wondering what ever happened to branch_1, it is still in your repository and can remain there forever.

Merge a branch to master

Git makes it easy to create new branches, switch the current branch you want to work on and merge specific branches to master.

Gitstorage is the perfect device for people developing new software or considering alternatives to cloud git repositories.

Data Driven Investor

from confusion to clarity, not insanity

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade