Skip to main content
March 23, 2026/3 min read

Git: How to Handle Merge Conflicts

Master Git Merge Conflicts with Expert Solutions

Understanding Merge Conflicts

Merge conflicts occur when Git cannot automatically reconcile differences between two commits. This typically happens when the same lines of code have been modified in different branches.

Common Merge Conflict Scenarios

Feature Branch Integration

When merging a feature branch back into master, conflicts arise if the same files were modified in both branches. This is the most common scenario developers encounter.

Team Collaboration

Multiple developers working on the same codebase simultaneously can create conflicts when their changes overlap in the same files or code sections.

Hotfix Conflicts

Emergency fixes applied to production branches can conflict with ongoing development work, requiring careful resolution to maintain code integrity.

What to Do with a Merge Conflict

1. When you encounter a merge conflict, Git will halt the merge process and clearly identify the file(s) that contain conflicting changes. Take careful note of these files—you'll need to resolve each one individually before proceeding. 2. Open the conflicted file in your code editor and locate the conflict markers that Git has automatically inserted:

<<<<<<< HEAD Marks the beginning of your current branch's changes (the branch you're merging into).

======= Acts as a divider, separating your changes from the incoming changes in the other branch.

>>>>>>> branch-name Marks the end of the conflicting section, showing changes from the branch being merged in.

3. Now comes the critical decision: how to resolve the conflict. You have several strategic options depending on your project's needs:

  • Manual resolution: Edit the code directly to decide which version to keep, or craft a hybrid solution that combines the best of both approaches. This gives you maximum control but requires careful attention to detail. Always remove the conflict markers (<<<<<<, =======, >>>>>>>) after making your edits—leaving them in will break your code.
  • Automated resolution: If you're confident about which version to use wholesale, Git provides efficient commands to accept one side completely. This approach is particularly valuable when working in team environments where you need to make quick, decisive choices:
  • git checkout --ours example.html (When you're on the master branch merging a feature branch, --ours preserves the current master branch version, effectively rejecting the incoming changes)
  • git checkout --theirs example.html (Conversely, --theirs accepts the incoming feature branch version, overwriting your current branch's version of that file)
  • Pro tip: If you're uncertain which option gives you the desired result, test both commands sequentially. Run one, examine the file, then run the other and compare. This eliminates guesswork and saves significant time since you avoid manual editing and marker removal—the commands automatically deliver clean, conflict-free files.

4. Once you've resolved all conflicts, stage your changes with git add, commit them with a descriptive message, and push to your remote repository if needed. Your merge is now complete and your codebase remains stable.

Merge Conflict Resolution Process

1

Identify Conflicted Files

Git will list all files with conflicts. Take note of each file that needs attention before proceeding with resolution.

2

Locate Conflict Markers

Open conflicted files and find the HEAD, equals, and branch-name markers that divide the conflicting code sections.

3

Choose Resolution Strategy

Decide whether to manually edit the code, keep one version entirely, or create a combination of both versions.

4

Stage and Commit Changes

After resolving conflicts, stage the modified files and commit the merge to complete the process.

Conflict Marker Reference

HEAD marks the start, equals divides versions, and branch-name marks the end. Always remove these markers after resolving conflicts.

Git Checkout Strategies

Featureourstheirs
Usage ContextKeep current branch versionKeep incoming branch version
Command Examplegit checkout --ours file.htmlgit checkout --theirs file.html
Best ForPreserving master branch changesAccepting feature branch updates
Recommended: Test both commands to see which version you want to keep if you get confused between ours and theirs.

Manual Editing vs Checkout Commands

Pros
Manual editing allows precise control over final code
Can combine best parts of both versions
Checkout commands eliminate need to remove conflict markers
Automated approach reduces human error
Cons
Manual editing is time-consuming for large conflicts
Risk of leaving conflict markers in code
Checkout commands are all-or-nothing decisions
May lose valuable changes from rejected version

Merge Conflict Resolution Checklist

0/6

Go Beyond Git

Mastering version control is just one piece of building a successful development career. Whether you're looking to advance your current role or transition into tech entirely, our comprehensive coding programs provide the hands-on experience and expert mentorship that today's employers demand.

Available Programming Courses

Web Development Classes NYC

Learn full-stack development through hands-on projects. Build real applications with modern frameworks and industry best practices from expert instructors.

Python Programming Classes NYC

Master Python fundamentals and advanced concepts. Work on practical projects that demonstrate real-world programming skills and problem-solving techniques.

Data Science Classes NYC

Explore data analysis, machine learning, and statistical modeling. Apply data science techniques to solve complex business problems with industry-standard tools.

Learn Through Real-World Projects

Our coding bootcamps and classes focus on practical experience with expert instructors guiding you through industry-relevant projects that build your professional portfolio.

Key Takeaways

1Merge conflicts occur when Git cannot automatically reconcile differences between branches, requiring manual intervention to resolve conflicting changes
2Conflict markers include HEAD for start, equals for division, and branch-name for end - these must be removed after resolution
3Manual editing allows precise control over conflict resolution by combining or choosing specific parts of conflicting code versions
4Git checkout with ours flag keeps current branch version, while theirs flag accepts incoming branch version for entire files
5Testing both ours and theirs commands helps determine which version to keep when the choice is unclear
6Checkout commands eliminate the need to manually edit files and remove conflict markers, providing a cleaner automated approach
7Always stage and commit changes after resolving conflicts to complete the merge process properly
8Professional coding education through bootcamps and classes provides structured learning with real-world projects and expert instruction

RELATED ARTICLES