Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019
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.
- View code in private projects: At least Basic access. - Clone or contribute to code in private projects: Member of the Contributors security group or corresponding permissions in the project. - Set branch or repository permissions: Manage permissions permissions for the branch or repository. - Change default branch: Edit policies permissions for the repository. - Import a repository: Member of the Project Administrators security group or Git project-level Create repository permission set to Allow. For more information, see Set Git repository permissions.
- View code: At least Basic access. - Clone or contribute to code: Member of the Contributors security group or corresponding permissions in the project.
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.
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.
Padoms
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.
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.
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.
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.
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.
Verify the merge options and then click Merge.
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. To resolve conflicts, choose Conflicts to open the Resolve Conflicts view.
The Resolve Conflicts view lists the files with merge conflicts. Select a file from the list to view the resolution options for that file.
For a file with merge conflicts in its content, select Merge to open it in the merge editor.
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.
For a file that was edited in one branch and deleted in the other, select which branch action you want.
In the Resolve Conflicts view, choose Commit Merge after you've resolved all merge conflicts for all files.
In the Changes view of Team Explorer, enter a commit message and choose Commit Staged to complete the merge.
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.
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.
Padoms
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:
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:
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.
Pievienojieties meetup sērijai, lai kopā ar citiem izstrādātājiem un ekspertiem izveidotu mērogojamus AI risinājumus, kuru pamatā ir reālas lietošanas gadījumi.
Respond to comments and complete pull requests in Azure Repos. Learn about completion options, auto-completion, and abandoning or reverting pull requests.
Learn how to discard uncommitted changes, revert changes in shared commits, reset a branch to a previous state, and generally undo changes in a Git repo.