When collaborating with others on a Git repository, merge conflicts can occur when Git is unable to automatically merge two or more conflicting branches or commits.
These conflicts arise when different changes are made to the same lines of code or when files are modified in conflicting ways.
This is what a merge conflict error on Git typically looks like:

Resolving merge conflicts is a crucial skill for maintaining a clean and functional codebase.
In this article, we will explore the best solutions ranked from the most effective to the least, enabling you to successfully resolve merge conflicts in your Git repository.
Manual Conflict Resolution
One of the most effective ways to resolve merge conflicts is through manual conflict resolution.
This approach allows you to carefully review the conflicting files, identify the conflicting sections marked by Git, and manually modify the code to create a resolved version.
Example:
$ git status
Identify the files with merge conflicts listed in the output of git status
.
$ git diff <file-path>
Inspect the conflict markers in the specified <file-path>
.
These markers highlight the conflicting sections that need to be resolved.
Manually edit the file to resolve the conflicts, removing the conflict markers and retaining the desired changes.
$ git add <file-path>
After resolving the conflicts, stage the modified file using git add
.
Git Merge Tool
Git provides a built-in merge tool that can assist in resolving conflicts.
The merge tool presents a visual interface that simplifies the process of resolving conflicts by highlighting the conflicting sections and providing tools for merging changes.
Example:
$ git mergetool
Running this command launches the configured merge tool (such as vimdiff, kdiff3, or meld) to help resolve conflicts.
The merge tool displays the conflicting sections side by side, allowing you to choose and merge the desired changes interactively.
Git Abort Merge
In some cases, it may be necessary to abort the ongoing merge process to resolve conflicts.
This option discards the changes from the conflicting branch and restores the repository to its pre-merge state.
Example:
$ git merge --abort
Executing this command cancels the merge in progress, discards all the changes from the conflicting branch, and restores the repository to the state before the merge started.
Git Stash
If you have uncommitted changes that interfere with the merge process, you can temporarily save them using git stash
before resolving the conflicts.
After resolving the conflicts, you can apply the stashed changes back to your working directory.
Example:
$ git stash
This command saves your uncommitted changes.
$ git stash apply
After resolving the conflicts, apply the stashed changes using git stash apply
.
Similar Questions Our Readers Have Asked
Which git tool resolve merge conflicts?
The Git tool that helps in resolving merge conflicts is the built-in merge tool.
This tool provides a visual interface that aids in resolving conflicts by highlighting the conflicting sections and offering tools for merging changes.
It simplifies the process of manually resolving conflicts by presenting a side-by-side comparison of the conflicting sections and allowing you to interactively choose and merge the desired changes.
The specific merge tool used can be configured in your Git configuration settings, and popular options include vimdiff, kdiff3, and meld.
Examples of using the in-built Git mergetool
Certainly! Here are a few examples of how to use the built-in Git merge tool to resolve conflicts:
- Launching the Merge Tool:
$ git mergetool
Running this command launches the configured merge tool and opens the first conflicting file. The merge tool’s interface will vary based on the tool you have set up in your Git configuration.
- Resolving Conflicts Using vimdiff:
$ git mergetool --tool=vimdiff
This command specifically launches the merge tool using vimdiff
as the tool. Vimdiff provides a side-by-side comparison of conflicting sections, making it easier to edit and merge changes.
- Resolving Conflicts Using kdiff3:
$ git mergetool --tool=kdiff3
Executing this command launches the merge tool using kdiff3
as the tool. Kdiff3 offers a graphical interface that presents a clear visual representation of the conflicts, allowing you to merge changes efficiently.
- Resolving Conflicts Using meld:
$ git mergetool --tool=meld
This command launches the merge tool using meld
as the tool.
Meld provides a user-friendly interface with side-by-side comparisons and visual highlighting of conflicts, simplifying the resolution process.
When you run the git mergetool
command with the desired tool option, the merge tool interface will open, displaying the conflicting sections and providing options to choose and merge the desired changes.
Use the tool’s instructions to navigate, select changes, and save the resolved file.
Remember to configure your preferred merge tool in your Git settings by using the git config
command. For example:
$ git config --global merge.tool vimdiff
This sets vimdiff
as the default merge tool.
Using the built-in merge tool offers a convenient way to visually resolve merge conflicts and streamline the conflict resolution process in Git.
How to resolve merge conflicts in git without commit?
When resolving merge conflicts in Git, it’s possible to complete the resolution process without immediately committing the changes.
Here are a few examples of how to resolve merge conflicts in Git without committing:
- Manual Conflict Resolution:
$ git status
Identify the files with merge conflicts listed in the output of git status
.
Open the conflicting file(s) in a text editor and manually resolve the conflicts by editing the conflicting sections.
$ git add <file-path>
After resolving the conflicts, stage the modified file(s) using git add
.
Repeat the above steps for each conflicting file.
- Using Git’s In-Built Merge Tool:
$ git mergetool
Launch the configured merge tool, which provides a visual interface for resolving conflicts.
Manually resolve the conflicts using the merge tool’s features and save the changes.
- Using Git Stash:
$ git stash
If you have uncommitted changes that interfere with the merge process, use git stash
to temporarily save them.
Resolve the merge conflicts by manually editing the conflicting files or using a merge tool.
$ git stash apply
Apply the stashed changes back to your working directory.
- Using Git Checkout:
$ git checkout --ours <file-path>
Choose “our” version of the conflicting file during conflict resolution. This discards the changes from the other branch and keeps your version.
$ git checkout --theirs <file-path>
Choose “their” version of the conflicting file.
This discards your changes and keeps the other branch’s version.
Manually resolve conflicts in each file using the above commands. By utilizing these methods, you can resolve merge conflicts in Git without immediately committing the changes.
Remember that once conflicts are resolved, it’s essential to review and test the changes before committing them to ensure a clean and functional codebase.
How to ignore merge conflicts in git?
In Git, it’s not possible to directly ignore merge conflicts, as they require manual resolution to ensure code integrity. However, you can configure Git to automatically use a specific strategy for resolving conflicts during merges. Here are some examples:
- Using “ours” or “theirs” Strategy:
$ git merge --strategy-option=ours <branch-name>
This command merges <branch-name>
into the current branch, resolving conflicts by favoring “ours” (the current branch’s version) for all conflicting files.
$ git merge --strategy-option=theirs <branch-name>
This command merges <branch-name>
into the current branch, resolving conflicts by favoring “theirs” (the other branch’s version) for all conflicting files.
- Using Recursive Strategy with “ours” or “theirs”:
$ git merge -s recursive -Xours <branch-name>
This command merges <branch-name>
into the current branch using the recursive strategy, resolving conflicts by favoring “ours” (the current branch’s version) for all conflicting files.
$ git merge -s recursive -Xtheirs <branch-name>
This command merges <branch-name>
into the current branch using the recursive strategy, resolving conflicts by favoring “theirs” (the other branch’s version) for all conflicting files.
Please note that while these strategies can automatically resolve conflicts by favoring one side, it is crucial to use them judiciously. Blindly ignoring merge conflicts can lead to loss of important changes or introduce inconsistencies in your codebase.
It’s generally recommended to manually review and resolve conflicts by carefully examining the conflicting files, understanding the changes made in each branch, and creating a well-informed resolution that retains the desired modifications.
Remember, resolving conflicts ensures code correctness and collaboration among team members, resulting in a more stable and reliable codebase.
What causes merge conflict in git?
Merge conflicts in Git occur when Git is unable to automatically merge changes from different branches or commits due to conflicting modifications made to the same lines of code or files. Several situations can lead to merge conflicts. Here are some examples:
- Conflicting Line Modifications:
When two branches modify the same line(s) of code, Git is unable to determine which change should take precedence, resulting in a merge conflict. For instance:
Branch A:
int count = 10;
Branch B:
int count = 20;
- File Level Conflicts:
Conflicts can arise when two branches modify the same file but in conflicting ways. For instance, if Branch A renames a file, and Branch B makes changes to the file’s content, a conflict will occur during merging. - Conflicting File Deletions and Renames:
If one branch deletes a file while another branch modifies or renames the same file, Git encounters a conflict when merging the changes. - Divergent Commit Histories:
Merge conflicts can occur when two branches have diverged, meaning they have made different and conflicting changes to the same set of files. - Incorrect Automatic Merging:
In some cases, Git may attempt an automatic merge, but the result does not meet the expected outcome due to conflicting changes. This can happen when Git cannot identify the appropriate changes to merge automatically.
It’s important to note that conflicts are an inherent part of collaborative development, particularly when multiple developers are working on the same codebase. Understanding the causes of merge conflicts helps in proactive conflict resolution and maintaining a clean and consistent codebase.
How to resolve merge conflicts in git pull request visual studio?
To resolve merge conflicts in a Git pull request using Visual Studio, you can follow these steps:
- Open the Pull Request:
Open Visual Studio and navigate to the repository where the pull request exists. Locate the pull request you want to resolve merge conflicts for and open it. - Fetch and Checkout the Pull Request Branch:
Click on the “Fetch” button in the pull request window to fetch the latest changes from the remote repository. Then, click on the “Checkout” button to switch to the pull request branch locally. - Resolve Conflicts:
Visual Studio provides a built-in merge tool to help resolve conflicts. In the “Changes” window, locate the files with conflicts and double-click on each file to open the merge tool. Within the merge tool, you will see the conflicting sections marked with both versions. Manually edit the code to choose the desired changes or use the merge tool’s features to select the appropriate modifications. Save the resolved changes for each conflicting file.
- Mark Conflicts as Resolved:
After resolving conflicts for all the files, go back to the “Changes” window in Visual Studio. Right-click on each file with conflicts and select “Mark as Resolved” to indicate that the conflicts have been addressed. - Commit and Push the Changes:
With the conflicts resolved, commit the changes to complete the merge process. Add a meaningful commit message that explains the resolution. Then, click on the “Commit” button in Visual Studio to commit the changes. After committing, you can push the changes to the remote repository by clicking on the “Push” button. - Close the Pull Request: Once the conflicts are resolved and changes are pushed, return to the pull request window in Visual Studio. Refresh the pull request page to ensure that your changes are up to date. If all conflicts are resolved, you can add a comment or review, and then click on the “Complete” or “Merge” button to close the pull request.
By following these steps, you can effectively resolve merge conflicts in a Git pull request using Visual Studio’s built-in merge tool, commit the changes, and close the pull request once conflicts are resolved.
Wrapping Up
Merge conflicts are a common occurrence when working collaboratively with Git.
Resolving these conflicts efficiently is crucial for maintaining a smooth development workflow.
By following the approaches outlined in this article, including manual conflict resolution, utilizing Git’s built-in merge tool, aborting the merge process when necessary, and leveraging the power of Git stash, you can confidently tackle and resolve merge conflicts in your Git repository.
Remember, clear communication and collaboration with your team members are essential during conflict resolution.
Happy coding with Git!

Abhinav worked as a software engineer at numerous startups and large enterprises for over 12 years. He has worked on a variety of projects, from developing software to designing hardware. He is passionate about tinkering with computers and learning new things. He is always looking for new ways to use technology to solve problems and make people’s lives easier. That is the inspiration behind https://foxrunsoftware.net. Abhinav created FoxRunSoftware to address common errors and issues faced by engineers and non-engineers alike!