Git Branch – Learn how to create, delete, and list branches.
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.
The above diagram shows two branches, B1 and B2, from the master.
Technically, what is a branch?
At first glance, it may appear that the 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 a program’s thread with access to all global data but its execution stack.
What can you do with a branch?
A branch has the same capabilities as the main line. You can create another sub-branch and perform commits, merges, pushes, etc.
The master branch is created with the repository creation. After that, you can create your first fork. Along with creation, it provides a complete branch management cycle.
Management involves creating new branches, merging existing ones, deleting them, and listing them.
The Mainline also continues to progress, 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 to create a branch in Git? With a step-by-step example.
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 username and password. We have our central codebase on machine 192.168.1.159.
# git clone firstname.lastname@example.org:/myrepository.git Initialized empty Git repository in /home/gittutorial/myrepository/.git/ email@example.com'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.
How to list down all branches?
Check to see if there is an existing branch. If not, we will create one.
[root@CentOS_6_64-160 myrepository]# git branch * master
[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 switches 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 hash code. The git log command displays all commits made along with their hash code. Users can obtain the hash code corresponding to the new branch’s beginning point.
[#]git branch new-branch 55206d46ae57179c117a74e10da797dcbd15177a
How to delete an existing 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 cannot do so because it 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