Git and Github

Git Tutorial 5: Branches in git? Check, Add, Delete, switch and merge Git Branches

Git Branches, Add delete, switch, and merge Branches

In the previous git tutorial, we have seen that how we can delete and ignore files from the Git repository. Now in this lesson, we will introduce branches in git. We will learn what is branches, how to list all of your branches, how to add a new branch, add, change and merge branches.

Branches in git?

Branch is one of the importance of feature available in the modern version control system, it represents an independent line of development. Branches allow co-workers to work on the different component of any software and merge all the component into the master branch as and when they complete.

In Git repository, a branch is a lightweight and moveable pointer and by default, Git assigns Master branch and set the pointer at initial commit. If you want to implement some new feature or some code that you are not sure whether it is going to be part of your codebase or not then you can add a new branch. With branches, we can mess with our code and test some new code and feature without affecting our original master branch. Later we can merge those changes into our master or original branch if we want those changes in our project.

Git – List Branches:

If you want to list down all the branches in your working git repository then you have to run a very simple command on your git repository. open up the command prompt in your git repository and write the following command:

>git branch  

Here is the output:

Branches in git - how to check the list of git branches

Now you can see that there is an asterisk followed by the name of the branch that we are currently in. so right now we have only one branch which comes by default. We will be using the git branch command to list branches that we have,  we can change branches back and forward and then merge the branch to master one.

Git – Add a Branch:

If we want to make some changes to our code without affecting the original master branch. So to do that we will create a new branch. Open up the console in your working git repository and write the following command to create a new branch.

>git checkout -b newbranch 

So when you write the above code then it will automatically switch you to newly created branch. So now we have another branch called ‘newbranch’

branches in git : how to create new git branch

If we want to list down all the branches then we will write git branch command in the command prompt working directory.

check list of branches

You can also notice that now we are inside ‘newbranch’

Git – Branch Checkout:

Checkout is used to switch branch, currently, we are inside newbranch. If we want to go back to the master branch from newbranch then we will write the following command.

>git checkout <branch name>  

In our case, we will write a master in the branch name.

how to switch between git branches

Now you can notice that the branch is switched back to master. If we want to confirm then we can write git branch in the terminal.

list down all the branches

You can notice that the master is green that’s mean we are currently inside master.

Git – Merge a Branch:

Right now, we have two branches master and newbranch and every branch have some commit and some code files. If you want to merge two branches then the first thing we need to do is go to the main branch (in our case we have master branch) and then run the following command to merge that branch into your main branch.

>git merge newbranch  

If we run the above command in the terminal it will merge the ‘newbranch’ branch in the master branch

merge two branches

What it did it bridged the gap here by merging these two branches together and then it commits which will tie all the code and changes together. So our newbranch branch merged into the master branch and it represented by new commit Merge branch ‘newbranch’. If we want to check to commit log then we can check by writing ‘git log’ command in the terminal.

Check the log of commit git branches

Git – Remove a Branch

So now if we want to remove any branch that we do not want any more. Here is the command that you can run on the command line to delete the unwanted branch.

>git branch –d <branch_name>       

In our case, we have two branches one is ‘master’ and another is ‘newbranch’. If we want to completely remove newbranch from our git hen we will write newbranch in the branch_name.

how to delete branch

Now our newbranch has been deleted. We have only one branch left which is the master branch.

Conclusion:

In this Git tutorials, we have given an introduction to branches in git. We have seen that how we can list down all the branches in git, add and delete branches, merge branches and switch branches.

 

32 Comments

Most Popular

To Top