Git branching simplified
Tags: github, git, beginners, tutorial
Published: 2021-12-24 16:13:45



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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rpr3w7wuagm8cpujcinz.png)
and we only have a single branch in our repo 
![github image ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3cjnfdpsgubogamzyc8.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gt0o99qgf9ayjemu2bg4.png)
Ok now we are on our feature branch now let’s add a new file to it 
![addednewfile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9j8djv7tp825rkx0n95i.png)
 So now we have added a new file, now let’s commit and push it 
 ![added commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6f1eo187dmz2v49ego6.png)
 Now use `git push --set-upstream origin addfeature` to set the remote branch to `addfeature `
![push](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kiuuv8ppcl4dstv4amt3.png)
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 `
![main](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bd4u072pgplnuvqc3lm.png)

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

 ![pull request ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wamgq24odbgbbkmdo3ng.png)
 now lets merge the pull request 
![merge pull](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ypw4opmqq9bmxr0dj2e.png)
now if you see the main branch, it have the new file.txt
![after pull](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3j67o0gkopumhj3r9fmj.png)
 
## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f38fqabu8bolx5fkp9xv.png)

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 ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8792tis83hkq0jyxzcaw.png)
![remote github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/os8f61ws5o76ll8ii71w.png)
  
and if you want to delete the newfile.txt i.e. what you have, merged from the branch use `git revert <commitid>` 
![commit id](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dm9tscargmvx6t3klw0b.png)
so my commit id was `b7d4b23` so I will use `git revert b7d4b23`
![revertd commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3gf1hhb5jxmri742nqob.png)
 
![reverted commit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88b6rcg8nttqxk9zz3wg.png)

![commit done](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/azwpf4cwlxqjdmxj6m9m.png)
 see now we don't have any the new file.txt in our GitHub repo.

##conclusion
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 :)

##Connect
 <a href="https://linktr.ee/kumarkalyan">
  <img align="left" width="100px" src="https://cdn.jsdelivr.net/npm/simple-icons@v3/icons/twitter.svg" />
</a>