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.
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
|Email:||(Your email is not shared with anybody)|