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


Summary:
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.




Pull, Fetch, Checkout and Clone


git fetch : Fetches the specified branch, does not merge
git merge : Merges the specified branches.
git pull : Does a git fetch followed by a git merge
git checkout : Switches to a new branch
git checkout -b : Creates a new branch and switches to it
git clone: Used when creating a local repository for the first time from the remote repository.




What is the difference between
git pull origin master
and
git pull origin/master?


git pull origin master will pull latest master branch from origin remote and merge to current local. Simple!
git pull origin/master will merge local copy of master branch (which was brought in sync with origin remote during last git fetch) into current local.

Basically the behavior of pull is same in both the cases.
origin/master refers to local cached copy of master from origin and
origin master refers to the latest up-to-date master branch at origin.




Cool Tip

If you get stuck while doing all the above, run git branch -a.
This will show you exactly what’s going on with your branches.
You can see which are remotes and which are local and this helps considerably in keeping the sanity of the system.



Git Push


git push [remote] [branch] can be used to push the local branch to the remote.








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:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal