Creating git branches are an integral part of a devops workflow. It allows you and your colleagues to work on a project without affecting the main development branches. Also branches allow for a controlled integration of your work to the development branches. Last thing you want is to overwrite your buddy's hard work correcting a bunch of bugs in the app!
To get started, I will be using git as my version control software. Install git on your system and continue with this guide.
Please have git installed before continuing further.
Let's hop onto git in the command-line!
Create a Project
Go ahead and create a local project. (My project is called hellokitty)
git init hellokitty
Create a README file and fill it with whatever content you want, I'm going to fill mine in with the current date.
date > README.md
Once the README FILE is all set, we'll add the add the addition and then commit it.
git add -A
git commit -m "created README.md"
Now, let's check our git branches! To see the branches, you can do a git branch or what I prefer -
git branch -avv.
scleft@ubu-dev:/hellokitty# git branch -avv * master 14dfa13 Created README.md
It's showing us that there is only one branch called master and that we are currently on it. Also you can see the last commit message and the unique hash generated for it. Lastly "*" indicates which branch you're on, extremely helpful once we have multiple branches in our project.
Create a test branch
To create the branch of our project, we'll follow the syntax below:
git checkout -b [localreponame]
This will create a branch for us and move us into the new branch. I will call my new branch "test".
git checkout -b test
Now if we check the branches (
git branch ) we should see two branches for our project. One for master and the other for test.
scleft@ubu-dev:/hellokitty# git branch -avv master 14dfa13 Created README.md * test 14dfa13 Created README.md
Looking good! There are two branches for our project and we are currently on test. To switch between branches simply type in
git checkout [branch_name] .
While we are in the test branch, create another file called numbers.txt and fill it with random numbers.
echo "1234567890" > numbers.txt
You should now have two files on your test branch. Go ahead and commit those changes.
Now let's check out the magic of branches, let's say you want to check what the original branch (master) looked like before you started your work on the test branch. To do that, you would need to do git checkout master .
As you can see below, the master branch only has the README.md present. While in the test branch both README.md and numbers.txt are present.
scleft@ubu-dev:/hellokitty# ls README.md
scleft@ubu-dev:/hellokitty# ls README.md numbers.txt
Awesome now you know how to create your own branch on a project and move around to different branches. Branches are extremely important when it comes it continuous integration! You want the ability to create multiple branches for your testing and development needs. Git provides us a way to do that effortlessly.