Git Branches

Harsh Seksaria
Version Control System, Git and GitHub

--

Hey there! Hope you’re doing well.

In the last blog, we learned about how to commit changes, amend an already made commit, see commit logs, and learned what are the parts of a commit when you see it in the log. In this blog, we are going to learn about branches in git.

Whenever you create a new repo, the default branch created is called “main”(called as master in older git versions; however, you can easily change them and the new version of git still supports master). A branch basically contains a set of changes/commits or we can say that it's a flow of changes. A branch can also be said as the records of the commits.

Branches can be more understood with the help of this figure:

Git Branch visual explanation
Image source: https://www.atlassian.com/git/tutorials/using-branches/git-merge

Like you can see in the figure above, we have the default branch “master” here, which is also referred to as the common base. This base is the most important since it includes the entire history of the repo. The circles along the line represent a commit in that branch. We can create as many branches as we want. The master or main usually contains the most updated or deployed piece of code, and other commits during the development phase can be made in new branches, but not necessarily. A new branch can be created at any time and from any other branch.

To interpret the above figure, we can say that “There is a main branch in the repo. A new branch has been created from main in the starting itself. There have been two commits in the main branch and three commits in the new branch.”

New branches can be made for a number of reasons, such as:

  1. Say, different teams are working on a project, like front-end team, server scripting team, etc. Each team can create their own branches in the repo, do all development work in it and when it works properly, merge into the main branch
  2. Say, your team is working on a new feature for the application, but it doesn't want to hamper the working flow, i.e. the current main branch, so they can create a new branch, experiment with the new feature, and commit that code into this new branch, and if it is a success, merge that branch into the main branch, etc…

One thing we observed in the above points is the merge. Merge means integrating two branches into a single branch. Like I gave an example above, let's say the new feature is working successfully, we can merge that branch into the main branch, and done. That new branch is now a part of regular code and the software now contains a new feature (doesn't mean that it's distributed to users). We will talk more about merge in the next blog.

To create a new branch

git branch <branch name>

To move to that branch / Switch branches

git checkout <that new branch name>

To verify, see that the main changes to the new branch’s name after the directory path in the command prompt.

To switch back to main, use git checkout main or master, whatever applicable.

To delete a branch

git branch -d <branch name>

You will get a message that the branch has been deleted.

To list all branches

git branch

In the next blog, we will discuss merging two or more branches. Till then, happy learning!

My name is Harsh Seksaria. I like to write about the topics simplifying them as much as possible for you to understand without unnecessary effort. Still, sometimes something may not be very clear. If you find any mistake, error, or you have a suggestion, just mail me. I’ll try to improve it asap.

My email: harsh20.freelance@gmail.com

My LinkedIn: www.linkedin.com/in/harshseksaria

--

--

Harsh Seksaria
Version Control System, Git and GitHub

MSc Data Science @ Christ (Deemed to be University), Bangalore, INDIA