Git Operations With Visual Studio, Part 2
In the continuation of this series, we look at how you can merge and fetch using Git commands in Visual Studio.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
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 Operation 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, and more. Now, in this article, I will explain how to merge the branches and resolve the conflicts if any.
Update Local Branch
Update the 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."
When you click "Sync," the window is changed to a new View. Now, click on "Fetch" to update the incoming commit list.
When you click on Fetch, you will see the incoming commits list.
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."
We will get a confirmation message.
Merge Branches
Go to Team Explorer and click on "Branches." It will change the view.
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.
Two things to note: You should always Sync your target branch (master) before merging. You must have the latest version of the branch to avoid another merge. Also, 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 do you merge changes? Well, Git stops for those changes and shows the conflicts.
Just click on the "Conflicts" menu.
It will display the list of files with conflicts. Click on each file, and then click on "Merge."
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
Click on the check-boxes, either source file, target file or both, depending on 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.
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.
Published at DZone with permission of Arvind Singh Baghel, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments