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 a great teacher for Git. He’s the one who created finderful, he could probably describe it’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. In this post, I will walk through the scenario of a real-life problem and show how Git branching can help solve the problem.
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.
Branching in Git
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
- Programmer 1: create the repository and merge the calculation logic into the master repo
- 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,
- git push remoteRepoName: Programmer 1 will push the repo to a remote location.
- git clone remoteRepoName: Programmer 2 will clone the remote repo locally e.g. git clone https://github.com/cptdanko/IonicStarterAppWithSwift.git
- git branch add : Programmer 2 creates a branch called add.
- git checkout add: then switches to the add branch to add the code/logic for addition to calculations.html.
- git commit -a -m “addition logic added”: commit the changes made to the calculations.html file i.e. the logic to add the numbers
- git checkout master: Switch back to the master branch.
- 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
- Create a file colors.html, such that it looks like this
- git init:initialise a git repo in the directory where colors.html is stored
- git add colors.html
- git branch blue
- git checkout blue: when in branch blue change the background-color:blue in colors.html
- git commit -a -m “mod color”
- git checkout master
- git branch red
- git checkout red: when in branch red change the background-color:red in colors.html
- git commit -a -m “mod color”
- git checkout master
- git branch yellow
- git checkout yellow: when in branch red change the background-color:yellow in colors.html
- git commit -a -m “mod color”
- 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.
- git merge blue
- git branch -D red: delete branch red
- 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
- Using branches
- Git Branching – Branches in a Nutshell
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'.
As usual, if you find any of my posts useful support me by buying or even trying one of my apps on the App Store.
https://mydaytodo.com/apps/
Also, if you can leave a review on the App Store or Google Play Store, that would help too.
Post Views: 804
0 Comments