Make delicious recipes!

Branching in Git

To create a branch, use the command:
git branch [branch-name]
This creates a new branch in git with the given name.

Note that the default branch in git is called master.
Many other VCSs use HEAD as the default branch name.
But in git, HEAD simply points to the current branch on which you are working.

Also note that the above command only creates a branch, does not switch to it.
After the above command, you are still on your previous branch.
To switch to the new branch, use the command:
git checkout [branch-name]
To switch to the master branch again, use the command:
git checkout master

To create a new branch and switch to it at the same time, use the command:
git checkout -b [branch-name]

Git creates a branch just by storing a pointer to the parent branch's snapshot.
Due to this, branching in git is almost instantaneous.
This is unlike other VCSs where branching means making copies of all the files.

Pictorial representation of a branch
                            child 2
grandparent <--- parent  <--- child 1 
                            child 3

In the above picture, gp, p, c1 denotes the master branch
gp, p, c2 and gp, p, c3 denote branches made from master branch from node parent.
HEAD is just a pointer to any of these branches and can be shifted by checkout command.

To see the last commit for every branch, run git branch -v

To delete a branch, use git branch -d [branch-name]

Branches on remote repositories

Branches on remote repositories are specified as [remote-name] / [branch-name]
For example: origin repository's master branch is referred to as origin/master
If a colleague creates a branch test from the origin, then that would be referred to as origin/test

origin/master refers to the master branch on the 'original' remote repository
Plain master refers to the master branch on the 'local' repository

It is helpful to remember that origin/master is just a pointer to the remote master set during local master creation and updated with each fetch/pull.

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal