Branching in GIT

Branching in git is a wide topic. It is one of its main great features. A local git repository is made in a directory using:

 git init 

This will create a branch, the default branch named master. To create new branches type this command:

 git branch branchname 

To switch to a branch :

 git checkout branchname 

Actually creation and switching can be done together by using the following command:

 git checkout -b branchname 

When you create a new branch it will contain all the contents in the master branch. But it will also contain some extra contents which is created inside

Current repository state

Current repository state

that branch which is not available from the master branch. So you are creating a copy of the master branch in which you make some certain changes which is not available in master branch. This is useful when you want to try modifying certain codes.

If you are working for a certain organization and you have its source code available from a certain remote repository. You clone it. And you encountered a bug and you want to make certain changes in the code. But not sure whether it actually works, then you make a new branch and make changes in it. After testing, if it is found that the changes made are indeed correct to resolve the problem, the branch is merged with the main branch. It’s simple.

So here we make a new branch Sample, after the first commit in the master branch and switch to it. It is done as follows:

 git checkout -b sample 
After creating the branch sample

After creating the branch sample

Now whatever commits we make, all will get stored in the working register of sample branch.

After switching back to the master branch, if we make any new commits it will get stored in the working register of the master branch and wont get reflected in the sample branch.

Commiting after switching back to master branch

Commiting after switching back to master branch

Any branch can be merged to the current branch by using the command git merge. Here we can merge the branch Sample to the main branch, Master. First switch to the master branch and then type the command:

 git merge sample 

Yea, its done. Now if you want to delete any branch then you can use the command,

 git branch -d <branchname> 

But one thing should be kept in mind when you are deleting the branch. If you want to delete a branch then you should first switch to some other branch. Or else it would be like cutting the branch of a tree in which you are sitting. So first you have to jump to some other branch of the tree and then cut the desired one.

There are many more stuffs that can be done using branches in git. This is just a beginning.

Know About GIT !

What is git?
Git is a free and open source distributed version control system.

Installation of git in Ubuntu:

$sudo apt-get install git 
You can check whether the installation is successful by:
$which git
Git records your name and email address with all your commits so first you have to do:
$git config --global user.name 'Your name'
$git config --global user.email 'Your email id'
and you can see the settings with this command:
$cat ~/.gitconfig

First to work with git you need a Git repository. Which can be made either by cloning from an existing public Git repository or by initialising a new reposiory in a directory using the command:

$mkdir Project

$cd Project

$git init 
This will create an empty repository in /path/Project/.git .
If you want to work on an existing project and use its source code then you can clone it by:
$git clone "https://github.com/username/repositoryname.git

git

The local repository consists of three “trees”:
1) Working directory – where the actual files are located.
2) Index – the staging area, ie where the changes are stored before the commit.
3) Head – It points to the last commit that has been made.

Commands in git :

$git add – It adds file to the staging area which is place to store the changes before commit.
$git status – It shows the status of the files in the working directory and staging area.
$git commit – Records the snapshot of the staging area, ie. all the changes are recorded. For adding the commit message while doing commit in the command line the following command is used:
$git commit -m 'My First Commit'
$git diff – Show the changes that have been made in the project after the last commit, which is not staged.
$git rm – It removes the file from the staging area which was added by using git add.
git2

The above things are done in a local repository. If it is to be shared among other people it should be added to a remote repository. To connect to a remote repository we use the following command:

$git remote add origin [url] 

To push the contents to the remote repository do:

$ git push -u origin master

$git fetch – Fetches new data from the remote server.

$git pull – It also do the same function as that of fetch. It differs by merging the data into the current branch.

git3

Git supports another great feature called branching. Branching helps in adding changes to the existing code without disturbing it. It has many use.  I will write about branching in git in another post.