Working with Git Branches in Visual Studio 2019

Continuing our previous posts, let us now see how we can work with branches within Visual Studio 2019.

Branches give the option to isolate the work from the original code base, this way we could have a developer(s) working on bug fixes, new features etc. which then can be merged back to the master branch later.

Open the Branches page in Team Explorer and click on the New Branch option.

Here we have created a new branch name MyFirstBranch from the master branch, and have also selected the option Checkout branch to work with that branch.

Create Branch creates the new branch as shown below.

To navigate to the branches page of Team Explorer, we can also use the Status bar of Visual Studio

Now let us make some changes in the program.cs file in the new branch created.

Do Commit and Push
All

From the team explorer, Branches page, we can see that the new branch has been added in the remote repository after commit.

To merge changes back from this new branch to the master branch, click on the master branch and select Merge option in the Branches page

  • Merge from Branch: MyFirstBranch
  • Into current branch: master.

As we have selected the option Commit changes after merging click on Merge pushes and sync the changes in the master branch in the remote repository as shown below.

We also have the options of deleting the branches as shown below, if they are not required anymore.

In this post, we saw how to work with Branches and how to merge it back to the base code.

Hope it helps..

Perform basic Git operations with Visual Studio 2019

In the previous post,

https://nishantrana.me/2019/10/21/getting-started-with-git-repository-in-visual-studio-2019/

we learned how to setup and  get started with Git Repository in Visual Studio 2019.

In this post, we will learn how to perform some of the basic operations like commit, push, fetch, pull, etc. and how to merge the conflicts.

Let us open the solution created in the last post and make some changes

Here we have updated Program.cs and added a new class file Helper.cs


To commit these changes, we can either go to Team Explorer – Home – Changes page or click on the edit icon in the status bar of the Visual Studio.

After we have added our commit comments, we are presented with 3 different options

Commit All Commit will make a record of the changes in the local repository.
Commit All and Push Commit the changes locally and push the changes to the remote repository.
Commit All and Sync Commit the changes locally, fetch the changes from the remote repository and finally push the changes to the remote repository

We can also use the Stage option to break the commit into smaller stages

We will be presented with similar options for Commit Staged as in the case of Commit.

Perform Commit Staged and Push for both the changes.

We can see both the updates in our remote git repository

Other developers or team members who need to work in the project can Clone the project in Visual Studio 2019.

Provide the Repository location URL along with the local path.

Now suppose the developer 1 makes the below changes to the program.cs and does commit all and push.

The other developer 2, can click on Fetch in the Synchronization page to see any incoming commits.

Clicking on the commit shows the details of the changes as shown below.

Developer 2 can click on Pull to merge commits to the local master branch.

fetch Download the changes from remote repository
merge Applies the changes fetched to a branch in the local repository
pull fetch + merge

Now suppose developer 1 has changed the message and commit + push the changes to the remote repository.

And also, in parallel developer 2 has also updated the same message and has clicked on Commit and Push.

As both have made changes and referencing to the same line of code, the developer 2 gets the failure message when he tries to push his changes to the remote repository.

Message in the output window à

Developer 2 here can click on Fetch to see the changes

Developer 2 clicks on Pull to merge the changes and can see the conflict.

Clicking on Conflicts gives Developer 2 the option to Merge, Take Remote or Keep Local the changes.

Selecting the Merge option allows developer 2 the option to specify the merge.

Clicking on Commit Merge to merge the changes, followed by appropriate Commit operation to push the changes to the repository.


This resolves the conflict.

Thus, in this post, we learned how to perform basic operations like Push, Pull, Fetch, etc. and how to merge and resolve the conflicts using Visual Studio 2019.

Hope it helps..

Getting started with Git Repository in Visual Studio 2019

Let us go through the steps to set up a Git Repository in Visual Studio 2019.

Open Visual Studio 2019

Go to Tools à Option and Set Git as source control plugin (if not set)

To create a new Repository, select Home àProjects and New Repository.

Provide the path and click on Create.

Click on the repository created to open it in the Team Explorer.

Click on Changes, which shows the files added as part of the creation of the Git Repository.

Click on Commit Staged to add the files to the local repository.

To sync it to the remote repository, we can click on Sync.

Now to add the solution to the Git Repository, back in the Home page of team explorer, click on New for the Solutions.

Follow through the Wizard. Here we have added a new solution of type Console Application to it.

Again, go back to Changes and click on Commit All.

To see the history, go to Home – Branches page, right-click on the master branch and select View history.

It shows the details of the 2 commits done locally in the local git repository.

Now to push the changes to Remote Repository

Click on Sync.

Click Publish Git Repo

Provide the URL of the Remote Repository.

Here we have created a new repository.

https://github.com/

Copy its URL.

Specify the URL and Publish the changes to the remote repository.

After successful publishing, we can see the files added in our remote GitHub repository

In Team Explorer – Home – Branches we can see the remote branch created.

To work with an existing remote repository within Visual Studio 2019, we can go to start page, select Clone or check out code option.

Either provide the URL or sign in to the GitHub account and choose the repository to clone.

Within Team Explorer also we can Clone the repository by using the Clone option

In this post, we saw how we can create a local repository, add the solution to it, publish it to the remote repository.

Hope it helps..