codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
📦

Git & GitHub

10 / 63 topics
9Branching and Merging10Resolving Merge Conflicts11Using git stash12Using git rebase
Tutorials/Git & GitHub/Resolving Merge Conflicts
📦Git & GitHub

Resolving Merge Conflicts

Updated 2026-04-20
3 min read

Resolving Merge Conflicts

Merge conflicts are a common occurrence when working with version control systems like Git, especially in collaborative environments. They happen when two or more people make changes to the same part of a file and then try to merge those changes together. This tutorial will guide you through understanding merge conflicts, how they occur, and how to resolve them effectively.

Understanding Merge Conflicts

What is a Merge Conflict?

A merge conflict occurs when Git is unable to automatically combine changes from different branches because the same lines of code have been modified in both branches. Git marks these areas in the file with conflict markers, which you need to manually resolve before completing the merge.

Common Scenarios for Merge Conflicts

  1. Parallel Changes: Two developers modify the same line or section of a file simultaneously.
  2. File Deletions and Additions: One branch deletes a file while another adds it.
  3. Branches with Divergent Histories: Branches have evolved independently, leading to conflicts when merged.

Steps to Resolve Merge Conflicts

1. Identify the Conflict

When a merge conflict occurs, Git will pause the merge and mark the conflicting files. You can identify these files by running:

git status

This command will list all files with conflicts, typically showing them as "unmerged paths."

2. Open Conflicted Files

Open each conflicted file in your text editor. Git marks conflicts with special conflict markers (<<<<<<<, =======, and >>>>>>>). Here’s an example:

<<<<<<< HEAD
function greet() {
    console.log("Hello, World!");
}
=======
function greet() {
    console.log("Hi there!");
}
>>>>>>> branch-name

3. Resolve the Conflict

You need to manually edit the file to resolve the conflict. Decide which changes to keep or how to combine them. For example:

function greet() {
    console.log("Hello, Hi there!"); // Combined message
}

After resolving conflicts in all files, remove the conflict markers.

4. Mark Conflicts as Resolved

Once you have resolved all conflicts in a file, mark it as resolved by staging the changes:

git add <file-name>

Repeat this step for each conflicted file.

5. Complete the Merge

After resolving all conflicts and staging the changes, complete the merge with:

git commit

Git will open your default text editor to allow you to edit the commit message. You can usually accept the default message or modify it as needed.

Advanced Conflict Resolution Techniques

Using a Merge Tool

For complex conflicts, using a visual merge tool can be more intuitive. Git supports various merge tools, such as KDiff3, Beyond Compare, and P4Merge. To configure a merge tool, use:

git config --global merge.tool kdiff3

Then, resolve conflicts with:

git mergetool

This command will open the configured merge tool for each conflicted file.

Using Git’s Built-in Conflict Resolution

Git provides some built-in commands to help manage conflicts:

  • git rerere: Automatically remembers how you resolved previous conflicts and applies the same resolution in future conflicts.

    Enable it with:

    git config --global rerere.enabled true
    
  • git diff3: Uses a three-way diff to show changes from both branches and the common ancestor.

Best Practices for Avoiding Merge Conflicts

  1. Frequent Commits: Make small, frequent commits instead of large ones. This reduces the likelihood of conflicts.
  2. Regular Pulls: Regularly pull changes from the main branch into your feature branch to keep it up-to-date and minimize conflicts.
  3. Clear Communication: Communicate with team members about changes you are making, especially in shared files or critical sections of code.
  4. Feature Branches: Use feature branches for new features or bug fixes. This isolates changes and reduces the risk of conflicts when merging back into the main branch.

Conclusion

Merge conflicts are an inevitable part of collaborative development with Git. By understanding how they occur, knowing how to resolve them effectively, and adopting best practices, you can minimize their impact on your workflow. Remember, resolving conflicts is a skill that improves with practice, so don’t hesitate to tackle them head-on whenever they arise.

By following this comprehensive guide, you should be well-equipped to handle merge conflicts in Git & GitHub, ensuring smoother collaboration and more efficient development processes.


PreviousBranching and MergingNext Using git stash

Recommended Gear

Branching and MergingUsing git stash