How do I resolve merge conflicts in a Git repository?
Categories:
Mastering Git Merge Conflicts: A Comprehensive Resolution Guide

Learn to effectively identify, understand, and resolve merge conflicts in Git, ensuring smooth collaboration and maintaining a clean project history.
Merge conflicts are an inevitable part of collaborative development when using Git. They occur when two or more developers make changes to the same lines in the same file, or when one developer deletes a file that another developer modified. While initially daunting, understanding the underlying causes and having a systematic approach to resolution can turn a potential roadblock into a routine task. This article will guide you through the process of identifying, understanding, and resolving Git merge conflicts, helping you maintain a clean and efficient workflow.
Understanding Git Merge Conflicts
A merge conflict arises when Git cannot automatically reconcile divergent changes between two branches. Git is smart enough to merge changes that occur in different parts of a file, or even changes to the same line if they are identical. However, when the same lines of code are modified differently in two branches, or when one branch modifies a file that another branch deletes, Git pauses the merge process and asks for human intervention. This is Git's way of saying, "I don't know which change is correct; you need to tell me."
flowchart TD A[Start Merge] --> B{Git detects conflicting changes?} B -- No --> C[Automatic Merge Successful] B -- Yes --> D[Merge Conflict Occurs] D --> E[Git pauses merge, marks conflicts] E --> F[Developer manually resolves conflicts] F --> G[Developer stages resolved files] G --> H[Developer commits merge] H --> I[Merge Complete]
Flowchart illustrating the Git merge conflict resolution process.
Identifying and Navigating Conflicts
When a merge conflict occurs, Git will typically notify you in your terminal and mark the conflicting files. If you're using a GUI client, it will highlight the conflicting files. Inside the conflicting files, Git inserts special markers to delineate the conflicting sections. These markers are crucial for understanding what changes came from which branch.
<<<<<<< HEAD
This is the content from the current branch (HEAD).
=======
This is the content from the branch you are merging into.
>>>>>>> feature/new-feature
Example of Git conflict markers in a file.
<<<<<<< HEAD
marker indicates the beginning of the conflict and the content from your current branch (HEAD). The =======
marker separates the changes from the two branches. The >>>>>>> branch-name
marker indicates the end of the conflict and the content from the branch you are merging.Resolving Merge Conflicts Step-by-Step
Resolving a merge conflict involves manually editing the conflicting files to choose which changes to keep, or to combine them in a way that makes sense. After editing, you inform Git that the conflict is resolved by staging the file and then committing the merge.
1. Initiate the Merge
Start the merge operation, for example, by running git merge <branch-name>
from your target branch. Git will notify you if conflicts occur.
2. Identify Conflicting Files
Use git status
to see a list of files that have merge conflicts. These files will be listed under 'Unmerged paths'.
3. Edit Conflicting Files
Open each conflicting file in your text editor. You will see the <<<<<<<
, =======
, and >>>>>>>
markers. Manually edit the file to remove these markers and keep only the desired code. You might choose to keep changes from one branch, discard changes from the other, or combine them.
4. Stage Resolved Files
After resolving the conflicts in a file, stage it using git add <file-name>
. Repeat this for all conflicting files.
5. Commit the Merge
Once all conflicts are resolved and staged, commit the merge using git commit
. Git will usually pre-populate a merge commit message; you can accept it or modify it if needed.
Advanced Resolution Techniques
Sometimes, conflicts can be more complex, involving file renames, deletions, or a large number of changes. Git provides tools to help in these situations.
# Abort a merge in progress
git merge --abort
# Use a merge tool (e.g., KDiff3, Meld, VS Code)
git mergetool
Useful Git commands for managing merge conflicts.
git merge --abort
will stop the merge process and revert your repository to the state it was in before you started the merge. This is useful if you get overwhelmed and want to start over.