Git Operations With Visual Studio - Part Two 🚀

Introduction

 
Today, I will show some more advanced Git operations using Visual Studio, without using the Git command line tool. This is the second part of the "Git Basic Operations With Visual Studio" series. Please read the first article here. In the previous article, we have seen Git basic operations like creating repository and branch, cloning, commit, push changes etc. Now, in this article, I will explain how to merge the branches and resolve the conflicts if any.
 

Update Local Branch

 
Update local repository/Branch to get changes from other members who have already made changes and merged. To update the code to keep it synced with others, there are three operations that come into the picture. 
  • Fetch: Fetch is used to get the changes from the remote repository but it does not merge them in your code.
  • Merge: Merge is used to apply changes taken from Fetch to a branch in the local repository.
  • Pull: Pull is the combination of two operations - Fetch and then Merge. 
We can get the changes to the local branch from the remote server using Fetch. Fetch downloads all the commits and new branches from the remote repository which others have pushed but you do not have. It also creates local branches if needed, but will not merge any changes into the local branches. Fetch only downloads the new commits.
 

Fetch: Download the changes

 
To fetch the changes, go to Team Explorer and click on "Home", then click "Sync".
 
Git Operations With Visual Studio
 
When you click "Sync", the window is changed to a new View. Now, click on "Fetch" to update the incoming commit list.
 
Git Operations With Visual Studio
 
When you click on Fetch, you will see the incoming commits list.
 
Git Operations With Visual Studio
 

Merge: Update Branches

 
We have downloaded the changes using the Fetch operation. Now, let us merge these changes into the local branch. When we do Sync or Pull from the Changes view, it will perform a merger.
 
To merge the downloaded changes into your branch, go to Team Explorer and click on "Home". Then, click on "Sync".

Git Operations With Visual Studio
 
 
Git Operations With Visual Studio
 
We will get a confirmation message.
 
Git Operations With Visual Studio
 

Merge Branches

 
Go to Team Explorer and click on "Branches". It will change the View.
 
Git Operations With Visual Studio
 
Click on "Merge" and you will have to indicate -
  • The branch you want to merge from
  • If you want to commit changes after merging, it will merge on your current branch.
Git Operations With Visual Studio
 
Note 1
You should always Sync your target branch (master) before merging. You must have the latest version of the branch to avoid another merge.
 
Note 2
Remember your merged branch is only local until you push modifications.
 

Resolve conflicts

 
When modifications on the several branches are contradictory, you will face some conflicts while merging. Git is super smart to understand the merges and changes. It resolves the conflicts by reading the history in your repository to determine the final version for the file but when it is not clear to the Git, then how to merge changes? Well, Git stops for those changes and shows the conflicts. 
 
Just click on the "Conflicts" menu.
 
Git Operations With Visual Studio
 
It will display the list of files having conflicts. Click on each file, and then click on "Merge".
 
Git Operations With Visual Studio
 
To help you resolve the conflict, Visual Studio will open the conflict resolving tool.
 
The tool will contain -
  • The file content of the branch you want to merge From (Source)
  • The file content of the branch you want to merge To (Target)
  • The result of the merger
Git Operations With Visual Studio
 
Click on the check-boxes either source file, target file or both, which you want to keep. Check other conflicts also to click on Next/Previous Conflict to navigate amongst conflicts of the files.
 
Finally, click on "Accept merge".
 
When you are done with all the files and don’t have conflicts anymore, you can click on "Commit merge", and then, stage your changes and commit as a regular commit.
 
Git Operations With Visual Studio
 

Conclusion

 
In this article, we have seen Git operations which we can perform with Visual Studio. I have shown Fetch and Merge operations and the process of merging to different branches. Also, we saw how to resolve conflicts after a merge.


Similar Articles