Sdílet prostřednictvím


Resolve merge conflicts in Visual Studio

Applies to: yesVisual Studio noVisual 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.

Diagram that shows a merge conflict.

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.

Diagram that shows how a merge commit resolves a merge conflict.

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.

    Screenshot of merge conflict after a push.

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

    Screenshot of the options available when the local branch is behind the remote branch.

    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.

    Screenshot of a merge conflict notification.

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

    Screenshot of merge conflict status in the Git Changes window.

  • In Merge Editor, start resolving your conflict by using any of the following methods (as depicted in the numbered screenshot):

    1. Go over your conflicts line by line, and choose between keeping the right or the left side by selecting the checkboxes.
    2. Keep or ignore all of your conflicting changes.
    3. Manually edit your code in the Result window.

    Screenshot that shows how to resolve a merge conflict in Visual Studio 2022.

    Tip

    If you don't like the default layout in Merge Editor, feel free to change it by using the gear dropdown menu.

    Screenshot of the Merge Editor layout options.

    For example, the following screenshot shows what the vertical view looks like:

    Screenshot of vertical view in the Merge Editor user interface.

  • When you're done resolving the merge conflicts, select Accept Merge. Repeat this process for all conflicting files.

    Screenshot of the Accept Merge action in Visual Studio 2022.

  • Use the Git Changes window to create a merge commit and resolve the conflict.

    Screenshot of how to create a merge commit by using the Git Changes window.

    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.

    Screenshot of the Keep Current and Take Incoming menu options.

Next steps

To continue your journey and learn more about resolving conflicts, see the Git webpage for the merge command.

See also