DEV Community

Cover image for Git branching simplified
Kumar Kalyan
Kumar Kalyan

Posted on

Git branching simplified

Hi everyone, hope you guys are doing well, today in this article I will be explaining the branching concept in git. If you love this article share with your friends and collogues.

Why branching

Branching helps you to work isolated in a team, Let's understand this thing with an example
Imagine a team of 4 members a,b,c and d respectively are working on a particular project. Now a is given a task to fix a bug, b and c is given a task to implement a new feature and d is the team lead. Now a will create a branch named bugfix and b and c will work on different branches and after the work is done d will review all the code in different branches and in the and at last merge everyone's code to the master branch. Now a question may arouse that, why are we using branches here? Suppose a has not fixed the bug properly mean while b and c have made the implementation properly so it become easier for the team lead to review everyone code and merge it to the master one (the code in the master branch is used for the production)

How to create a branch & what happens then

I have a repository named github tutorial now let’s see what happens when we create a branch use git branch <branch name >

See now we are on the main branch having 3 files in it
the main branch
and we only have a single branch in our repo
github image

Now lets create abranch name addfeature
So we will use git branch addfeature followed by git checkout addfeature shifting to our new branch

branch created
Ok now we are on our feature branch now let’s add a new file to it
So now we have added a new file, now let’s commit and push it
added commit
Now use git push --set-upstream origin addfeature to set the remote branch to addfeature
see we have added a file to our add feature branch and now we are getting an option for compare and pull i.e merging the code
you can see that our main branch does not have the newfile.txt

now let's compare and make a pull request and then merge our code to the master branch

pull request
now lets merge the pull request
merge pull
now if you see the main branch, it have the new file.txt
after pull

deleting a branch

Now we have merged our code to our master branch but now I feel that we don't need this addfeature branch so let's delete it and see what happens
use git branch -d <branch name >
delete branch locally

this command will delete our branch from our local git repository, now to delete this branch from GitHub use git push <remote> --delete <branch>

remote github

and if you want to delete the newfile.txt i.e. what you have, merged from the branch use git revert <commitid>
commit id
so my commit id was b7d4b23 so I will use git revert b7d4b23
revertd commit

reverted commit

commit done
see now we don't have any the new file.txt in our GitHub repo.


So this was all about the branching concepts in git, feel free to comment out if you have any queries, hit a ❤️ if you love this article . stay tuned for next, Happy Coding :)


Top comments (0)