Git Branch – Learn how to create, delete, and list branches.


What is SMS and how it works in telecom?

Why does branching necessary? Each software development team maintains a master branch or mainline of code. There is always a possibility that a new feature should be developed and committed to the repository without causing any disruptions to the mainline.

What is a git branch
What is a git branch?

The above diagram shows that there are two branches B1 and B2 have been created.

Technically, what is a branch?

At first glance, it may appear as though that branch is simply a copy of the complete code, but it is not. Git maintains code references that are valid for specific branches. Consider it as a program’s thread that has access to all global data but has its own execution stack.

What you can do with a branch?

A branch has the same capabilities as the mainline. You can create another branch from an existing one, perform commits, merges, and pushes, and so on.

The master branch is created after the repository is created in git. Now you can create your first fork. Along with creation, git provides a complete branch management cycle.

Management involves creating new branches, merging existing ones, deleting them, and listing them.

The Mainline continues to progress as well, with more commits being made in the mainline. once the new branch’s development finishes, it is merged into the mainline and removed.

NOTE: There can be further branching from a branch.

How can i create a branch in git? Step-by-step examples?

We’ll assume that we’ve created a repository with git init and have access to a master branch.

Clone the repository.

Bring the copy of the central git repository on the local machine using the git clone command. It requires a user name and password. We have our central codebase on machine 192.168.1.159.

# git clone root@192.168.1.159:/myrepository.git
Initialized empty Git repository in /home/gittutorial/myrepository/.git/
root@192.168.1.159's password:
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.

List down all barnches.

Check to see if there is an existing branch. If not, we will create one.

[root@CentOS_6_64-160 myrepository]# git branch
* master
Till now only the master branch is present.  With the git branch command, we will create a new branch named branch1 and again list branches.
[root@CentOS_6_64-160 myrepository]# git branch branch1
[root@CentOS_6_64-160 myrepository]# git branch
branch1
* master

As you can see, there are two branches, master and branch1. The current working branch is master, the git checkout command changes control to another branch.

If a user makes changes and commits them, they will appear in branch1 only and the head of branch1 will be moved, but the master will still point to the original head.

The following example demonstrates how to switch from master to branch1.

[root@CentOS_6_64-160 myrepository]# git checkout branch1
Switched to branch 'branch1'
[root@CentOS_6_64-160 myrepository]# git branch
* branch1
master

Create a git branch based on a specific commit point:

There is a possibility that the master branch is not stable after a commit and a developer needs to branch over to a stable version for the new feature. Git includes the ability to create a branch from a specific commit.

Each commit has its own hash code. The git log command displays all commits made along with their hash code. Users can obtain the hash code corresponding to the point at which the new branch will begin.

[#]git branch new-branch 55206d46ae57179c117a74e10da797dcbd15177a

How to delete an exesting branch?

Git includes the ability to delete a branch. However, that branch should not be a working copy. If a user attempts to delete the current branch, git returns the error “Cannot delete the branch <branch name>, which is the current branch.” To begin, the user must switch to a different branch using the git checkout command.

[#]git branch -d branch-name

The above command deletes locally.

Merging a branch with the master:

The git branch command supports merging a branch into the current branch. If branch1 needs to be merged into master, the following commands must be performed.

[#]git checkout master
[#]git merge branch1

How to push a branch to a central repository?

The branch created thus far is stored in the computer locally. If other developers wish to clone the branch, they are unable to do so because the branch is not visible in the remote/central repository.

To bring the branch to the remote repository, push the branch. To begin, use git checkout to switch to another branch. Then, use the command to push.

[#]git push origin branch-name