git branch

git branch

git branch command , is one the command from set of git commands, git provides this command for branch management. Management means creating a new branch, deleting a branch, merging branches, switch branch etc. We assumes that we have created a repository using git init and have master branch to work with.

Why branching is required? Every software development group have a mainline of code, git named this master. There are always chances that new feature should be done and commit to repository without disturbing mainline. Mainline keeps also moving  along with more commits done in main line. When new branch and development work completes , branch is merged in mainline.

Before creating a branch ,clone the central git repository on local machine.

# git clone root@
Initialized empty Git repository in /home/gittutorial/myrepository/.git/
root@'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 the branches,
[root@CentOS_6_64-160 myrepository]# git branch
* master
Only master branch is present.  git branch create new branch named branch1
[root@CentOS_6_64-160 myrepository]# git branch branch1
[root@CentOS_6_64-160 myrepository]# git branch
* master
Now you can see that there are 2 branches, git checkout command, switches to a particular  branch
[root@CentOS_6_64-160 myrepository]# git checkout branch1
Switched to branch 'branch1'
[root@CentOS_6_64-160 myrepository]# git branch
* branch1
If a user will do any changes and commit that will be in branch1 and head of branch1 will be moved , but master will be pointing to original head.

git branch create from a particular commit point:

There are chances that a master branch is still not stable and a developer need to branch the master over a stable version for new development. Git provides a make a branch from a particular commit. A commit have a unique hash code. Git log command is for displaying all commits done along with hash code. User can get hash code for the point from where new branch will start.

[#]git branch newbranch 55206d46ae57179c117a74e10da797dcbd15177a

git show branch , gives more detail information for all branches.

Deleting a branch :

Git provides option for deleting a branch. But that branch should not be a current working branch. If a user tries to delete the current branch git gives the error "Can not delete the <branch name> , is current branch". A user has to first switch the branch by using git checkout command.

[#]git branch -d branch-name

Branch merging :

Git branch command provides feature to merge a branch to the current branch. If branch one needs to be merge in master the commands are

[#]git checkout master

[#]git merge branch1

git remote branch :

The branch created till now is in local repository. If other developers want to make the clone from branch, they can not as branch is not visible in remote/central repository. To make branch in remote repository, then branch needs to push. First switch to new branch with git checkout then , do push with command.

[#]git push origin branch-name

Leave a Comment

Your email address will not be published. Required fields are marked *