How do I resolve merge conflicts in a Git repository?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Resolving merge conflicts in a Git repository involves several steps. Here’s a detailed guide on how to do it:
1. Initiate the Merge
When you try to merge two branches and Git encounters conflicting changes, it will pause the merge and mark the files with conflicts.
For example, if you are on the main branch and want to merge the feature branch:
If there are conflicts, Git will output a message indicating which files have conflicts.
2. Identify Conflicted Files
After a merge conflict occurs, you can see the files that are in conflict by running:
The output will list the files with conflicts under the section Unmerged paths.
3. Open the Conflicted Files
Open the files that have conflicts in a text editor or an IDE. Git will mark the conflicts in the file with special conflict markers.
Example of a conflict:
<<<<<<< HEAD: Marks the beginning of the conflicting changes from the current branch (HEAD).=======: Separates the conflicting changes.>>>>>>> feature: Marks the end of the conflicting changes from the branch you are merging (feature).
4. Resolve the Conflict
Manually edit the file to resolve the conflict. You need to choose which changes to keep, or combine the changes in a way that makes sense.
For example, after resolving:
After editing, remove the conflict markers (<<<<<<<, =======, and >>>>>>>).
5. Mark the Conflict as Resolved
After resolving the conflict in each file, you need to mark it as resolved by adding the file to the staging area:
Replace <file> with the name of the file you just resolved.
6. Complete the Merge
Once all conflicts have been resolved and added to the staging area, complete the merge by committing the changes:
Git will automatically create a commit message indicating that it is a merge commit. You can customize the commit message if necessary.
7. Push the Changes (if necessary)
If you're working on a remote branch, you'll need to push the resolved changes:
8. Automating the Process (Optional)
Some IDEs and text editors, like Visual Studio Code, PyCharm, or IntelliJ IDEA, offer tools that help you resolve conflicts more easily by providing a visual interface.
Summary
- Merge branches: Attempt to merge the branches using
git merge <branch>. - Identify conflicts: Run
git statusto see which files are in conflict. - Resolve conflicts: Open the files, manually edit to resolve conflicts, and remove conflict markers.
- Mark as resolved: Use
git add <file>to mark the conflict as resolved. - Commit the merge: Complete the merge with
git commit. - Push changes: Push the changes to the remote repository if needed.
Handling Complex Conflicts
- Abort the Merge (if needed): If you decide you don’t want to resolve the conflicts and instead want to abort the merge, you can do so with:
- Using External Tools: For more complex merges, you can use external merge tools like
Meld,KDiff3, orBeyond Compare, which integrate with Git and provide a visual way to handle conflicts.
Handling merge conflicts can be tricky, but by following these steps, you can carefully resolve conflicts and complete your merge successfully.

