Resolve merge conflicts in Visual Studio
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
When you merge one branch into another, file changes from commits in one branch can conflict with the changes in the other. Git attempts to resolve these changes by using the history in your repo to determine what the merged files should look like. When it isn't clear how to merge changes, Git halts the merge and tells you which files conflict.
Understand merge conflicts
The following image shows a basic example of how changes conflict in Git. In this example, the main branch and the bugfix branch make updates to the same lines of source code.
If you try to merge the bugfix branch into main, Git can't determine which changes to use in the merged version. You might want to keep the changes in the main branch, the bugfix branch, or some combination of the two. Resolve this conflict with a merge commit in the main branch that reconciles the conflicting changes between the two branches.
The most common merge conflict scenario occurs when you pull updates from a remote branch to your local branch (for example, from origin/bugfix into your local bugfix branch). You can resolve these conflicts in the same way: create a commit on your local branch to reconcile the changes, and then complete the merge.
Prevent merge conflicts
Git is good at automatically merging file changes in most circumstances, as long as the file contents don't change dramatically between commits. If your branch is far behind your main branch, consider rebasing your branches before you open a pull request. Rebased branches will merge into your main branch without conflicts.
Resolve merge conflicts
If you're collaborating with others in the same branch, you might get merge conflicts when you push your changes.
Visual Studio detects if the local branch you've been working on is behind its remote tracking branch and then gives you options to choose from.
Note
If your remote repository supports Force Push, you can enable it by using Git > Settings.
In this example, select Pull then Push to include changes introduced to the remote repository. If there are any merge conflicts when you're pulling changes or trying to merge two branches, Visual Studio lets you know in the Git Changes window, in the Git Repository window, and on any files that have conflicts.
The Git Changes window shows a list of files with conflicts under Unmerged Changes. To start resolving conflicts, double-click a file. Or if you have a file with conflicts opened in the editor, you can select Open Merge Editor.
In Merge Editor, start resolving your conflict by using any of the following methods (as depicted in the numbered screenshot):
- Go over your conflicts line by line, and choose between keeping the right or the left side by selecting the checkboxes.
- Keep or ignore all of your conflicting changes.
- Manually edit your code in the Result window.
When you're done resolving the merge conflicts, select Accept Merge. Repeat this process for all conflicting files.
Use the Git Changes window to create a merge commit and resolve the conflict.
Note
If you need to keep all of your changes to a file, you can right-click it in the Unmerged Changes section and select Keep Current (Local) without having to open Merge Editor.
Next steps
To continue your journey and learn more about resolving conflicts, see the Git webpage for the merge command.