Working with Git and Visual Studio – Using git branching

In previous blog post in this series, we discussed about concept of git branching. We saw how and why it is easy to create a new branch in Git compared to other versioning tools. We also discussed concept of HEAD in more depth and how it is useful to keep track of current status. In this blog post, we are going to explore more on the branching concept.

View current branch in Git and Visual Studio

This can be viewed simply by running either ‘git branch’ or ‘git log’ command. If we run first command, the current branch is highlighted by an asterisk(*) just next to it. Similarly, if we run second command, it will be highlighted by position of HEAD:

Get current checked out branch
Get current checked out branch

If we open our project in Visual Studio, Go to team explorer and then see branches, we would see something like below:

Viewing branches inside visual studio
Viewing branches inside visual studio

We can find that current branch is highlighted in more bold color.

Switch current branch in Visual Studio

Let’s say we want to switch back to master branch to work on some issue. In this case, we can checkout master branch by either double-click on it or by right click and then select checkout:

Switch branch inside visual studio
Switch branch inside visual studio

Once it switches, now master branch is highlighted in bold to inform that HEAD is now pointing to master branch.

Create new branch in Visual Studio

We have already learned how to create new branch using command line in previous post. If we need to create new branch in Visual Studio, we can do same from the new branch option:

Create new branch in Visual Studio
Create new branch in Visual Studio

It will ask us few things like name of new branch, base branch you want to copy from and if you want to checkout new branch at the same time:

Create new branch in Visual Studio - 2
Create new branch in Visual Studio – 2

The equivalent of creating new branch and simultaneously switching to it in git command line is using ‘git checkout -b ‘.

Do note that you can not switch to another branch if you have uncommitted changes or stashed changes that conflict with the branch you’re checking out, Git won’t let you switch branches. It’s best to have a clean working state when you switch branches.

Delete a branch in Git and Visual Studio

To delete a branch inside Visual Studio, we can simply right click the branch and then select delete:

Delete a branch inside Visual Studio
Delete a branch inside Visual Studio

Do note that we can delete current checked out branch. We can only delete other branches either by git or Visual Studio:

Cannot delete current checked out branch
Cannot delete current checked out branch

Visual Studio enforce this behavior by disabling the delete option from current checked out branch.

The equivalent for deleting branch using git command line is ‘git branch -d ‘ . One can also use -D which is equivalent of –delete –force:

Delete a branch inside git
Delete a branch inside git

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s