Resolve merge conflicts
Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019 | TFS 2018
Visual Studio 2019 | Visual Studio 2022
When you merge or rebase, you're telling Git to integrate changes made on one branch with changes made on another. Often, Git completes a merge or rebase automatically without your assistance. However, if Git discovers that a change made on one branch conflicts with a change made on another, it prompts you to resolve the conflict. A merge conflict can occur when the merged branches edit the same file line differently, or when one branch modifies a file and another branch deletes it. The process for resolving merge conflicts is applicable to both Git merge and rebase.
You can resolve merge conflicts in Visual Studio, or by using the command line and any text editor.
For an overview of the Git workflow, see Azure Repos Git tutorial.
This article provides procedures for the following tasks:
- Understand merge conflicts
- Resolve merge conflicts
Understand merge conflicts
Git merge or rebase integrates commits from a source branch into your current local branch (target branch). Git merge performs either a fast-forward or a no-fast-forward merge. The no-fast-forward merge is also known as a three-way merge or true merge. Git rebase is another type of merge. These merge types are shown in the following diagram.
For Git merge, if the tip of the target branch exists within the source branch, the default merge type will be a fast-forward merge. Otherwise, the default merge type will be a no-fast-forward merge.
A fast-forward merge can never have a merge conflict because Git won't apply a fast-forward merge if the tip of the target branch has diverged from the source branch. By default, Git uses a fast-forward merge whenever possible. For example, Git will apply a fast-forward merge on a local branch that you only update by pulling from its remote counterpart branch.
A no-fast-forward merge generates a new target branch "merge commit" that integrates source branch changes with target branch changes. The applicable changes are those made after the last commit that's common to both branches. In the preceding diagram, commit C is the last common commit in both branches. If any source branch change conflicts with any target branch change, then Git will prompt you to resolve the merge conflict. The merge commit (L) contains the integrated source branch and target branch changes. The source and target branch tips (K and E) are the parents of the merge commit. In your branch's commit history, a merge commit is a useful marker for a merge operation, and clearly shows which branches were merged.
Git rebase resequences the commit history of the target branch so that it contains all source branch commits, followed by all target branch commits since the last common commit. In the preceding diagram, commit C is the last common commit in both branches. Another way to view it is that a rebase replays the changes in your target branch on top of the source branch history. If any source branch change conflicts with any target branch change, then Git will prompt you to resolve the merge conflict. As with the fast-forward merge, a rebase doesn't create a merge commit. Notably, a rebase changes the sequence of the existing target branch commits, which isn't the case for the other merge strategies. In the preceding diagram, commit K' contains the same changes as K, but has a new commit ID because it links back to commit E instead of C.
Git merge and rebase only modify the target branch—the source branch remains unchanged. When you encounter one or more merge conflicts, you must resolve them to complete the merge or rebase. Or, you can cancel the merge/rebase operation and return the target branch to its prior state.
When to resolve merge conflicts
- Keep your local
mainbranch current with its remote counterpart by periodically pulling to fetch and merge remote commits.
- Integrate local
mainbranch updates into your local feature branch using a rebase or merge.
- Back up your work on the local feature branch by pushing it to the corresponding remote branch.
- On feature completion, create a pull request to merge your remote feature branch into the remote
By frequently integrating remote changes into your local repo, you can stay aware of recent work by others and promptly resolve any merge conflicts that arise.
Resolve merge conflicts
The process for resolving merge conflicts is applicable to both Git merge and Git rebase. Although the following steps describe how to resolve merge conflicts during a merge, you can similarly resolve merge conflicts during a rebase.
- Visual Studio 2022
- Visual Studio 2019 - Git menu
- Visual Studio 2019 - Team Explorer
- Git Command Line
Visual Studio 2022 provides a Git version control experience by using the Git menu, Git Changes, and through context menus in Solution Explorer. Visual Studio 2019 version 16.8 also offers the Team Explorer Git user interface. For more information, see the Visual Studio 2019 - Team Explorer tab.
In the Branches pane of the Git Repository window, checkout the target branch. Then right-click the source branch and choose Merge <source-branch> into <target-branch>.
Visual Studio will notify you if Git halted the merge due to conflicts. In that event, you can either resolve the conflicts, or cancel the merge and return to the pre-merge state. The Unmerged Changes section of the Git Changes window lists the files with merge conflicts. For a file with merge conflicts in its content, double-click the file to open it in the merge editor.
In the merge editor, the Incoming pane shows the source branch file version, the Current pane shows the target branch file version, and the Result pane shows the resultant merge file. To apply specific source or target branch changes, select the checkbox next to the conflicting line(s) that you want to keep. You can also directly edit the merge file in the Result pane. Choose Accept Merge after you've resolved all merge conflicts in the current file. Repeat this step for each file with content conflicts.
For a file that was edited in one branch and deleted in the other, right-click the file and select which branch action you want.
In the Git Changes window, enter a commit message and choose Commit Staged to complete the merge—after you've resolved all merge conflicts for all files.