Branches in Git



When you are working on your project, you may come across instances where you need to work in a separate workspace. Some of these situations can be:

  • When you are working on a new feature which may take some time and you do not want to push those changes to the main branch unless you test those features well
  • When you want to create a bug fix and want to test that fix in a separate branch before merging it to the main or master branch.
  • Your colleague is working on a different branch and you want to write some add-on codes to be used by your colleague.

You can achieve this by using Branches in Git. For this, you can just create a separate branch from the main branch (or any other existing branch), and start working on that branch without worrying about other team members are pushing to the main branch of the Git repository. Once you have completed the programming and testing of the new feature or bug fix, you can merge the changes of that branch into the parent branch.

Git Image



Creating a new branch

To create a new branch in Git, type the following command.

git branch new_branch_name
                    

Replace the keyword 'new_branch_name' with your preferred name of the branch, such as 'bugfix-dashboard-issue', 'feature-email-notification', etc.

Once you type this command, a new branch will be created. But if you notice carefully, you will see that your IDE will still be showing the files of the old branch (which was open earlier in your IDE).

This is because once we create a new branch, we need to switch to it to start working on that branch.



Switching to a new branch

To switch to the newly created branch, type the following command:

git checkout new_branch_name
                

Now, If you want to perform both the actions together, i.e. creating a new branch and switching to it, then you can do it directly using the below shortcut command.

git checkout -b new_branch_name
                    


Listing all branches

When you start creating new branches, you will have a lot of branches to manage. In such cases, if you want to view the list of all branches, you can use the below command:

git branch                        
                    


Deleting a branch

If you want to delete a branch, you can do so using the following command.

git branch -d branch_name                        
                    

Replace the keyword 'branch_name' with the actual name of the branch which you would like to delete.

While you are deleting a branch, you need to ensure that the changes are merged, otherwise you may get an error. If you do not wish to merge those changes and still want to delete the branch, you can use force delete operation.

git branch -D branch_name
                    

Notice that we are using the '-D' (uppercase) instead of '-d' (lowercase) which we used earlier.

Note: Force delete operation is not suggested for beginners, use only once you gain good understanding of Git operations.



Renaming a branch

Let us suppose you made some changes in your new branch and later realize that the branch name is not appropriate and you want to rename it, then you can rename the branch using the following command.

git branch -m branch-old-name branch-new-name
                    

Replace the branch's old name and new name with your preferred branch names.

Now, there is another way of doing that as well. You can simply create a new branch from the old branch and give a new preferred name to the new branch, and once it is created, you can delete your old branch. But, if you follow this approach, then ensure your changes in the local workspace is committed to your old branch before you create a new branch out of that, otherwise your local changes will be lost.



That's it for this tutorial. I hope you liked reading it, do share this tutorial with your friends.

Recommendations
FREE Sales CRM Software

Fully customizable CRM Software for Freelancers and Small Businesses

Signup for Free

Sign up for DigitalOcean Cloud

Get FREE $200 credits, deploy your hobby projects for Free

DigitalOcean Referral Badge
Sign up for Hostinger Cloud or VPS plans and get upto 20% Discount