Skip to main content

Branching in Git: simplified with examples


Distributed version control systems are a lot of fun to work with, and I just happen to know Git more than any of the others. I do know Mercurial but while I had to learn Mercurial on my own, I had(still have when I get stuck) a great teacher for Git. He's the guy who created SuburbChooser, he could probably describe SuburbChooser's purpose better than me. All I know is that it is built to help people  and it is powered by some pretty complex algorithms under the hood.  Anyway let's not lose focus, we are here to talk about why branching in Git is awesome and how can we benefit from it in our projects.

Why this post?

I am building a dieting app with this researcher who does research in, well; health and exercise? or so (I do not know the exact research topic). What I know is that she is an expert in this field and besides sounding smart when talking,  she has published research in the field to back up the smart talk. Anyway, so while she is a great researcher who happens to know Git, her Git branching skills are a bit rusty at this point, simply because it has been a while since she used it. So this post is just to help her brush up her knowledge and for me to have a document that I can come back to at any point.

So why branching?

So often in projects, I have to add new features to existing code base or improve existing code base by experimenting with alternatives and I would like to achieve these things without breaking what already works. So, in come branching to save the day and help me achieve both of the aforementioned things. The 2 scenarios outlined below, should make it much more clear of what I just described. 

Scenario 1

Ok so let us look at this hypothetical scenario where we are trying to perform an arithmetic calculation on 2 numbers and there are 2 programmers working on it.  The following are each of their duties
  1. Programmer 1: create the repository and merge the calculation logic into the master repo
  2. Programmer 2: add the logic for addition
Programmer 1 will create the master branch and add an html file called calculations.html and initialise a Git repository where it is stored by running the following command

git init

So Programmer 1, will push the git repository to a remote location like say Github or Bitbucket such that Programmer 2 can checkout the repository and do their work. Ok in this scenario, Programmer 1 will be busy with a whole lot of other things once, he creates the repo. So listed below are the sequence of steps to create and merge the branch that will have the logic to add the numbers with the master branch. At this point the code in the master branch looks like this,



  1. git push remoteRepoName: Programmer 1 will push the repo to a remote location.
  2. git clone remoteRepoName: Programmer 2 will clone the remote repo locally                        e.g. git clone https://github.com/cptdanko/IonicStarterAppWithSwift.git
  3. git branch add : Programmer 2 creates a branch called add
  4. git checkout add:  then switches to the add branch to add the code/logic for addition to calculations.html. 
  5. git commit -a -m "addition logic added": commit the changes made to the calculations.html file i.e. the logic to add the numbers 
  6. git checkout master: Switch back to the master branch. 
  7. git merge add: At this point it is assumed that Programmer 2 is happy with the logic in the add branch and on looking at Programmer 1 is happy for it to be part of the app, so Programmer 1 will merge the code in the add branch with the master branch. So the code in the master branch looks like this now 
So we successfully created a new branch added some code to it and merged it back to the master branch. 

Branch creation and checkout shortcut

For the sake of detail and getting a better understanding I have kept the entire branch creation and checkout a bit more verbose. But the 2 steps of creating and checking out a branch i.e. git branch branchName and git checkout branchName,  can be a simple 1 step process by running just one command 
git checkout -b branchName

Scenario 2 (includes branch delete)

Ok in this scenario let us say we have a div which is black in colour and we want change it but we are not sure what colour we want for the div, so we want to try a few before we decide. So let us see how branching with Git can help with this, we can try some of the steps below
  1. Create a file colors.html, such that it looks like this 
  2. git init:initialise a git repo in the directory where colors.html is stored
  3. git add colors.html
  4. git branch blue
  5. git checkout blue: when in branch blue change the background-color:blue in colors.html
  6. git commit -a -m "mod color"
  7. git checkout master
  8. git branch red
  9. git checkout red: when in branch red change the background-color:red in colors.html
  10. git commit -a -m "mod color"
  11. git checkout master
  12. git branch yellow
  13. git checkout yellow: when in branch red change the background-color:yellow in colors.html
  14. git commit -a -m "mod color"
  15. git checkout master: so at this stage we have looked at how all the colours and we decide that blue looks best. So what do we do? we merge blue with master branch and delete all the other branches.
  16. git merge blue
  17. git branch -D red: delete branch red
  18. git branch -D yellow

Conclusion and Further reading

So I hope the aforementioned scenarios show how easy it is to work with branches in Git and show how branching makes it easy to experiment with a bunch of solutions to a problem before deciding on a particular solution. For further reading, follow the links below
  1. Using branches
  2. Git Branching - Branches in a Nutshell

p.s.

So the master branch is created after you do your first commit after initialising your repository via git init . If by any chance try to create a new branch via  git branch name, before doing your first commit  you will get this error
fatal: Not a valid object name: 'master'.




Comments

Popular posts from this blog

Upload to AWS S3 from Java API

In this post, you will see code samples for how to upload a file to AWS S3 bucket from a Java Spring Boot app. The code you will see here is from one of my open-source repositories on Github, called document-sharing. Problem Let’s say you are building a document sharing app where you allow your users to upload the file to a public cloud solution. Now, let’s say you are building the API for your app with Spring Boot and you are using AWS S3 as your public cloud solution. How would you do that? This blog post contains the code that can help you achieve that. Read more below,  Upload to AWS S3 bucket from Java Spring Boot app - My Day To-Do (mydaytodo.com)

App update, discovering Protractor(testing) and an Angularjs State machine

So for the last couple of weeks, I have not be able to update my blog or finish a couple of the posts that I started writing. Well, I have...been a bit busy, with some the following things,  Testing my app I have been using the app( iOS ) on my iPhone for the past couple of months now, which is both good and bad. The good thing is I can test it and the bad thing is, I can use it every day. Part of the reason, I started building this app is to have something that I can and like to use everyday and since I am already using it, the incentive to release it is not as high as if it were something that I could not use. Anyway I did make some good progress over the last couple of weeks, like  Delete the app from my iPhone and do a clean install: this did help me find a few simple bugs that would pop-up when the app is first installed GET AN APP ICON, FINALLY! this was a bit of a hurdle and I thanks to Fiverr  and fivercrazyguy , I finally have my app icon. The day job ...

Why can't I cancel a local notification in my iOS app?(in Swift)

Prelude I have mentioned the fact that I am working on my first iOS app in a number of my previous posts and as such I face a number of newbie/noob(?) problems. They are not really problems as much as they are simply things that I do not know, for e.g. the following, how do I get the day of the week? where is my string.replaceAll in Swift? knowing which local notification brought my app to foreground ? serving HTML content in an iOS app that works on iOS 7 and above Thankfully, I have managed to make at least one generic solution and contribute it to the community i.e. my open-source Xcode project template( HTML5StarterAppWithSwift ) which you can get from Github . So this post basically describes another very simple problem that I have found a solution to, but I do not fully understand why the problem was occurring in the first place. Introduction As I have mentioned in this post , my iOS app uses local notifications i.e. UILocalNotification   and i...