Troubleshooting & Comparisons

How to Resolve Merge Conflict in Git Effectively

Step-by-step guide on identifying and resolving merge conflicts in Git, including explanations of causes and practical solutions.

5 min read

When working with Git for version control, encountering a merge conflict is a common challenge. Merge conflicts occur when changes made in different branches contradict each other, necessitating manual resolution. This guide helps developers identify merge conflict symptoms, understand their causes, and effectively resolve them to get back to building smoothly.

Identifying Merge Conflict Symptoms

Merge conflicts often disrupt the workflow but are usually easy to spot. Git clearly highlights conflicts in both the command line and the files themselves.

steps

  1. Run git merge or git rebase. If a conflict arises, Git will stop the process and display an error message similar to:

    bash
    error: merge conflict in <file-name>
    Automatic merge failed; fix conflicts and then commit the result.
  2. Check for conflict markers in your files. Open the files stated by Git (under unmerged paths in the git status output). The conflicting areas are surrounded by special markers:

    <<<<<<< HEAD
    Your changes
    =======
    Changes from the other branch
    >>>>>>> branch_name
  3. Run git status to list affected files. Files with unresolved conflicts will appear under the unmerged paths section:

    bash
    git status
    # Unmerged paths:
    #   both modified:   filename

Common Causes for Merge Conflicts

Merge conflicts in Git happen due to inconsistencies across branches. Here are some frequent scenarios that lead to conflicts:

  • Line edits by multiple branches: Two or more branches modify the same line in the same file.
  • File deletion vs. modification: One branch deletes a file, while another modifies it.
  • Out-of-sync changes: Unreconciled changes due to cherry-picking or rebasing.

Understanding the cause of the conflict can simplify the resolution process.

Resolving Merge Conflicts in Git

Resolve merge conflicts manually in a series of clear steps using tools like a text editor, Git GUI, or diff merge tools.

steps

  1. Open each conflicting file. Look for conflict markers (<<<<<, =====, >>>>>).

  2. Decide how to resolve the conflict. Choose the appropriate set of changes by:

    • Keeping your changes (HEAD section).
    • Keeping their changes (other branch section).
    • Combining the two.
  3. Manually edit the file. Remove the conflict markers and ensure the file looks exactly how you want it.

  4. Mark the file as resolved. Use the following command to stage the resolved file:

    bash
    git add <file-name>
  5. Commit the resolved files. Finish the process by committing the resolved files:

    bash
    git commit -m "Resolve merge conflict in <file-name>"
  6. Verify resolution success. Confirm resolution by ensuring no remaining conflicts are listed:

    bash
    git status

Using the Git Merge Abort Command

If the conflict resolution process gets too complicated or you decide to pause solving the conflict, Git provides a way to undo incomplete merges. This lets you restore the repository to its pre-merge state.

Utilizing Rebase to Preserve a Clean History

Rebasing helps avoid creating merge commits, allowing for a linear commit history. This strategy can maintain a tidy main branch, especially in collaborative projects.

  • Rebase replays changes from one branch onto another: This avoids cluttering your commit history with extra merge commits.
  • Ideal for local branches: Use rebase for feature branches or when preparing your branch for integration.
  • Avoid rebasing shared branches: Rewriting history for shared branches can cause issues for other team members working on those branches.

Run the following command to rebase a branch onto another:

bash
git checkout feature-branch
git rebase main

If a conflict arises during rebase, resolve it using the same process described earlier, then continue rebasing:

bash
git rebase --continue

Proper use of rebase can help keep your repository's history clean and understandable.

FAQ

What causes a merge conflict in Git?

Merge conflicts occur when two branches contain conflicting changes. Common causes include modifying the same line in different branches, deleting a file in one branch while editing it in another, or unaligned histories during cherry-picks or rebases.

How do I undo a merge if I can't resolve a conflict?

Use the git merge --abort command to cancel the merge and return the repository to the state before the merge attempt. For a rebase conflict, use git rebase --abort for the same effect.

Should I always use merge or rebase when resolving conflicts?

It depends on your project requirements. Use merge to preserve the full commit history and record the context of branch integration. Use rebase when you want to avoid merge commits and maintain a cleaner, linear history.

What tools can help resolve Git merge conflicts?

You can use any text editor, Git GUIs (e.g., GitHub Desktop, GitKraken), or dedicated merge tools (e.g., Meld, Araxis Merge). Configure tools using git config --global for seamless integration.


Official reference: Git reference manual.