Git Branching

Git Branching

An Introductory article about Branching in Git

Branching

Branching is the most important concept of Git. And it is because of branches we can manage the different versions of our code. We can take branches as separate versions of the main repository. These branches are a pointer to a snapshot of your changes. Basically, when we want to update a new feature or to remove a bug we don’t want to do it directly on the main repository.

Advantages of Branching

1) Editing the unstable code without Impacting the main branch.

2) On the completion of work, the branch can be merged to the main repository.

3) Fix the errors in a new branch.

4) Working on different branches by a team of developers and collaborating the code by merging it to the main repository.

5) Branches are lightweight and fast.

6) Branches don’t interfere with each other. It means each branch is independent of other branches.

Master Branch

The default branch in Git is a master branch in Git. The first time we initiate a commit command a master branch is created automatically to act as a starting point of the project. There can only be one master branch of a particular repository. All the changes made to the project are finally merged into the master branch.

Creating a new branch

Git branch my-branch

This command will create a new branch with the name “my-branch”.

List Branch

Branches can be listed by the following command:

 git branch --list

or

git branch

While listing the branches the symbol * next to the branch name, specifies the currently active branch.

Deleting Branch

You can delete the specified branch by the following command:

git branch –d my-branch

Git prevents you from deleting the branch if it has unmerged changes. “my-branch” is the name of the branch. We can use –D/-m in place of –d if we want to force delete the branch.

Branch Switching

Branch switching refers to changing the active branch. Git allows us to switch between branches even without making a commit. The command used is:

Git checkout my-switched-branch

After using this command the branch will be switched to the branch named “my-switched-branch”. You can switch to the master branch from any other branch with the help of the following command:

git branch -m master

If we use the -b option on checkout it will create a new branch, and move to it, if it does not exist.

Git Merge

The merge command allows us to merge other branches with the active branch. Git merge combines multiple sequences of commits into one unified history of the commit.

Let’s say we want to merge a new branch named update1 to the master branch. First, we need to make the master branch our active branch.

git checkout master

Now we can merge our updated branch ‘update1’ to the master branch

git merge update1

update1.jpg

Now if we want we can delete the update1 branch as it is already a part of the master branch.

git branch -d update1

I hope you like the article. Please share your feedback.

If you missed the first article of the series, please check it out : Git-ting Started

In the third article of the series, I will cover Introduction to Github.