Working with GIT – Part 2 (Working with branches)

While working with repositories, one would have come across the concept of branch. While in every CVS system, the meaning of branch is more or less the same, but they work differently in all of them. The git uses something which is called as a revolutionary way of handling the branch which makes it easy to use branches and merge them. We will learn how to work with branches in GIT.

Viewing all the branch in your current project

git branch

This will display all the branches in the project you are working on. The * beside a branch denotes the current active branch.

If this is a new project, or you are working with branches for the first time, then most probably you will find only a single branch named master with the asterisk. This would mean that there is only one branch and its the active one. The master branch is nothing but the first branch that git creates for us when the git init command is run.

Creating a branch

git branch newbranch

This will create a new branch named newbranch.

Switching to the new/another branch

While the above command git branch newbranch will create a new branch named newbranch, you will still be in the earlier branch you were working on. If you run git branch, you will still see that the asterisk wouldn’t be beside the newbranch, but would be beside the previous branch, most likely the * master branch.

So to switch, run the checkout command.

git checkout newbranch

Now if you run the git branch command, you will see that asterisk would have moved to the * newbranch, signifying that the current active branch is the newbranch. Now any changes made and committed would happen on the newbranch.

Read the first part Part 1 – Creating my first git project

Murari Mohan Nayak (Murari M.)

Leave a Reply