This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Download Microsoft Edge
More info about Internet Explorer and Microsoft Edge
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.
For more information on merge options and strategies, see the
Git reference manual
and
Git merge strategies
.
When to resolve merge conflicts
Git
merge
and Git
rebase
are extensively used in the
Git workflow
. When working on a local feature or bugfix branch, it's common practice to:
Keep your local
main
branch current with its remote counterpart by periodically
pulling
to fetch and merge remote commits.
Integrate local
main
branch 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
main
branch.
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.
If the source branch is a
remote-tracking
branch, ensure that branch is up-to-date by running a Git
fetch
before a merge. Or, run the Git
pull
command, which combines a Git fetch with a Git merge.
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.
Visual Studio 2019 provides a Git version control experience by using the
Git
menu,
Git Changes
, and through context menus in
Solution Explorer
.
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.
Visual Studio 2019 version 16.8 and later versions provides a Git version control experience while maintaining the
Team Explorer
Git user interface. To use
Team Explorer
, uncheck
Tools
>
Options
>
Preview Features
>
New Git user experience
from the menu bar. You can use Git features from either interface interchangeably.
In the
Branches
view of
Team Explorer
, checkout the target branch. Then right-click the source branch and choose
Merge From
.
In the merge editor, the
Source
pane shows the source branch file version, the
Target
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.
Before merging a source branch into a target branch, checkout the target branch. Then use the Git merge command to merge the source branch into the current local branch (target branch). To merge multiple source branches into a target branch, separate the source branch names with spaces.
git checkout <target branch>
git merge <source branch>
You can also use git switch <branch name> to switch to a branch.
Some scenarios will cause the Git merge command to fail with an error, or halt before completion.
Git message
Description
Resolution
Your local changes to the following files would be overwritten by merge...
This error indicates that your current local branch has one or more files with uncommitted changes, and those files are affected by the merge. Git won't generate an error for uncommitted changes in files that aren't affected by the merge.
Commit, stash, or discard your uncommitted changes, then retry the merge.
Automatic merge failed; fix conflicts and then commit the result.
At least one source branch change conflicts with a target branch change. Git halts the merge and waits for you to resolve the merge conflicts.
Cancel the merge by running git merge --abort, or resolve all merge conflicts then run git merge --continue.
For the current merge operation, you can:
Run git status to list the files with merge conflicts.
Run git log --merge to list the conflicting commits in the source and target branches.
Resolve file content conflicts
Open a file in any editor to view and resolve the conflicting text. Git added a set of markers to indicate instances of conflicting text. For example:
- Network controller
- Fiber optic transmitter
- Fiber optic transceiver
- Network switches
=======
- Network control
- Fiber optic transmitter
- Fiber optic transceiver
- Network switch
>>>>>>> origin/main
The text between <<<<<<< and ======= is the target branch version of the conflicting text.
The text between ======= and >>>>>>> is the source branch version of the conflicting text.
Edit the file to resolve the conflicting text. By removing the merge conflict markers, you're letting Git know that you've resolved the conflict. For example, you can change the prior text (including the conflict markers) to:
- Network controller
- Fiber optic transmitter
- Fiber optic transceiver
- Network switch
Run git add <filename> to stage the file once you've resolved all conflicting text.
Repeat the preceding steps for all files with content conflicts.
Resolve deleted-modified file conflicts
A deleted-modified file conflict occurs when the same file is edited in one branch but deleted in another. For each deleted-modified file, decide which branch action you want to go with. Then use git add <filename> to keep the modified file, or git rm <filename> to delete the file.
Next steps
Share code with push
Undo changes
Related articles
New to Git repos? Learn more
Update code with fetch, merge, and pull
Save your work with commits