GIT Branchand its Operations

GIT Branch and its Operations.

GIT is the most used and powerful among the Version Control Systems. GIT branch is an important feature of the modern Version Control System. We can say branches in GIT is like a logical pointer of all the set of changes that you have made in the code base. In easy words, GIT branch is the local copy of the main codebase which will have the changes of the new feature or bug fix, which will later be merged with the main codebase once after verified. So, Let’s deep-dive into the GIT Branch its Operations.

Table of Contents

GIT Branching

GIT Branching is the strategy or systematic procedure of creating the branch and follow that up for merging that with the main copy of the main branch of the codebase. Before getting into the GIT branch, Let’s see why we need a branch.

GIT Branch
GIT Branch

Above Image shows how branching is done in GIT for developing a new feature. Basically, A branch is the isolated and independent line of developing the feature. Assume the middle line as the master branch where the code is stable and updated. Then, assume a developer-1 is assigned to develop Feature – 1. So, Developer-1 is cutting a branch from the master branch which is indicated in the above diagram as in Magenta color. And, Developer-2 is assigned to develop Feature-2 and he is cutting a branch from the master branch. And, that is indicated as in Blue color.

GIT Branching and Merging
GIT Branching and Merging

Let’s assume the codebase is the set of four files called File-1, File-2, File-3, and File-4. So, when Developer-1 is taking the branch from master to develop feature-1 (assume File-1 and File-3 are going to be changed for the development). So, the developer can safely commit his changes on both files into the master branch. Vice versa, When Developer-2 is taking the branch from master to develop feature-2 (assume File-2 and File-4 are going to be changed for the development).

Unlike other Version Control system, Branching in GIT is logically copying the codebase and when it is getting merged to master branch, the only snapshot of the changes is appended from the feature branch to the master branch. But in other Version Control System like SVN, Branches are literally copied while cutting the branch and replaced while merging with the master branch.

GIT Branch Operations and its Commands

As GIT Branch is about maintaining the separate line of development, it has multiple operations. Following are the GIT branch operations.

  • Creating a New Branch
  • Deleting a Branch
  • Renaming the current Branch
  • Other Basic Operations

Let’s see detailed explanation on these operations.

Creating a New Branch

To create a new branch from the master branch, pass the following command.

git branch <branch_name>

So, this will create a new branch but still you are not switched to the created new branch. So, to switch to the new branch, enter the following command

git checkout <branch_name >

So, GIT has another option to create and switch to the new branch at the same time by passing the following command

git checkout -b <branch_name> 

Here the argument -b is to create the branch along with checking out the created new branch.

Deleting a Branch

Just like a file or a folder, the branch can be deleted. To delete a branch, we have two different methods.

git branch -d <branch_name>

In this the argument ‘-d’ means deleting the branch only if the branch is pushed and merged with the remote branch. So, this is the safe delete option.

git branch -D <branch_name>

So, the argument -D means deleting forcefully without checking whether the branch is pushed or not.

If you wish to delete the remote branch directly, pass the following command

git push origin --delete <branch_name>

Alternatively, we have short form of this command

git push origin :<branch_name>

Remember that origin in the above command is the remote name of the GIT server

Renaming the current Branch

Renaming a branch is another important operation in GIT. Especially, when a developer creates a branch and push it to remote and wanted to rename.

To Rename local branch

If you want to rename the local branch and you are in the branch which you want to rename mean, Pass the following command

git branch -m <new_branch_name>

If you are in different branch and want to rename the branch locally mean, Pass the following command

git branch -m <old_branch_name> <new_branch_name>

To Rename the remote branch by deleting

If you want to delete the remote branch and push the new branch from local mean

git push origin :<old_branch_name> <new_branch_name>

This is again the deleting the branch but along with the same command, we are renaming the branch by appending new branch.

Rename the branch by resetting upstream

If you are in the branch which needs to be renamed in remote, then pass the following command with upstream argument -u.

git push origin -u new-name

This will just rename the current upstream branch.

Other Basic Operations

Other than creating, deleting and renaming the GIT branch, we have a couple of other basic operations in the GIT branching

To list all local branch, Pass following command

git branch -a

If you want to List all remote branch, Use

git branch -r

for even more GIT operations, Check the documentation of GIT.

Conclusion

Among the Version Control Systems, GIT is having a different mechanism for branching. In this article, we have discussed what is GIT Branch and its Operations and how the commands of GIT Branch is been used. GIT can also be used as an automation tool if we can utilize and handle all the features of it. So, Let’s see more about GIT and its features in our upcoming article. Stay tuned and subscribe  DigitalVarys for more articles and study materials on  DevOps,  Agile,  DevSecOps and  App Development.

1 thought on “GIT Branch and its Operations.”

  1. There are some points that I don’t understand in this article, can they be clarified for other articles?

    Reply

Leave a Reply Cancel reply

深圳SEO优化公司宝鸡百姓网标王多少钱普洱设计网站多少钱酒泉网站搜索优化价格贵阳网站搜索优化临猗关键词排名包年推广报价潍坊企业网站建设报价黔西南模板网站建设价格驻马店外贸网站设计亳州关键词排名包年推广哪家好松原网站排名优化多少钱宿迁网站seo优化推荐长沙模板制作报价恩施网站设计推荐恩施seo价格白银企业网站改版塘坑英文网站建设揭阳网站优化软件报价陇南网站搭建推荐贺州网站开发报价大庆网站改版多少钱商丘百姓网标王推广金昌模板制作公司固原建网站多少钱渭南百度竞价多少钱庆阳企业网站建设哪家好长治如何制作网站价格包头百度关键词包年推广公司荷坳seo网站优化报价兴安盟阿里店铺运营公司清徐网站优化按天计费哪家好歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运

深圳SEO优化公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化