Perform basic Git operations with Visual Studio 2019

Sending
User Review
0 (0 votes)

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..