Skip to main content
Resolving Merge Conflicts
CHAPTER 05

Resolving Merge Conflicts Manually

Updated: May 15, 2026
25 min read

# CHAPTER 5

Resolving Merge Conflicts Manually

1. Introduction

Analysis is complete; it is time for action. Resolving a merge conflict manually requires no special tools, no expensive IDEs, and no complex Git commands. It requires only a basic text editor, an understanding of the code's business logic, and meticulous attention to detail. In this chapter, we will walk through the exact keystrokes required to delete Git's conflict markers, synthesize conflicting code, and formulate the final commit that successfully ends the "Merging" state and restores harmony to the repository.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Manually edit a file to remove Git conflict markers.
  • Choose between keeping Current changes, Incoming changes, or a synthesis of both.
  • Use git add to mark a conflict as resolved.
  • Conclude the merge process with a final commit.
  • Abort a merge entirely using git merge --abort.

3. Beginner Explanation

Imagine you are an editor finalizing a movie script.
  • The script has a conflict block. Writer A wrote: "The hero escapes in a car." Writer B wrote: "The hero escapes in a helicopter."
  • The Fix: You open a word processor. You are the boss. You delete the ugly Git markers around the text. You delete the line about the car. You keep the line about the helicopter.
  • You save the document.
  • You tell the publisher, "The script is fixed and ready to print" (This is git add).
  • You officially publish it (This is git commit).

4. The 4 Steps to Manual Resolution

When git status shows a file in red as both modified, you must follow this strict 4-step sequence to fix it.
  1. 1. Open & Edit: Open the file in a raw text editor. Manually delete the <<<<<<<, =======, and >>>>>>> lines. Keep only the code you want.
  1. 2. Save: Save the file to your hard drive.
  1. 3. Stage: Tell Git you have fixed the file by adding it to the staging area.
bash
123456789101112131415161718192021222324252627282930313233343536373839404142434445
   git add filename.txt
   ```
4. **Commit:** Finalize the merge process.
   ```bash
   git commit -m "Resolve conflict in filename.txt"
   ```
   *Note: As soon as you hit enter on the commit, the `(MERGING)` state disappears from your terminal. The crisis is over!*

### 5. The Three Choices of Resolution
When you edit the file, you have three distinct choices:
- **Accept Current (HEAD):** You delete the incoming code and keep what was already on `main`. You are rejecting your coworker&#039;s work.
- **Accept Incoming:** You delete the `main` code and keep the code from the feature branch. You are replacing the old code.
- **Synthesize (Accept Both):** You realize *both* developers wrote valid code. You delete the markers and manually arrange both lines of code so they work together.

### 6. Mini Project: Resolve PHP Project Merge Conflict
Let&#039;s resolve a simulated PHP code conflict manually.

**Step-by-Step Walkthrough:**
1. Imagine you open `database.php` and see this:
   ```php
   <<<<<<< HEAD
   $db = new PDO(&#039;mysql:host=localhost;dbname=prod', 'root', 'old_password');
   =======
   $db = new PDO(&#039;mysql:host=localhost;dbname=prod', 'admin', 'new_secure_pass');
   >>>>>>> feature-security-update
   ```
2. You recognize that the `feature-security-update` branch contains the correct, updated credentials.
3. Place your cursor on the `<<<<<<< HEAD` line and delete the entire line.
4. Delete the "old_password" line entirely.
5. Delete the `=======` line.
6. Delete the `>>>>>>> feature-security-update` line.
7. Your file should now look like perfectly clean PHP:
   ```php
   $db = new PDO(&#039;mysql:host=localhost;dbname=prod', 'admin', 'new_secure_pass');
   ```
8. Save the file.
9. In the terminal, type:
   ```bash
   git add database.php
   git commit -m "Resolve database credential conflict"
   ```

### 7. The Panic Button (`git merge --abort`)
Sometimes, a merge goes horribly wrong. You open a file and see thousands of conflict markers. You try to fix them, but you get confused and accidentally delete critical code. Your file is ruined, and you haven&#039;t committed yet.
**Do not panic.** You can instantly time-travel back to safety.

bash git merge --abort `` This command instantly cancels the entire merge process, deletes all conflict markers, and restores your files exactly to how they looked 5 minutes ago before you ever typed git merge. It is the ultimate "undo" button.

8. Best Practices

  • Test Before Committing: Just because you deleted the conflict markers does not mean the code actually works! If you merged two complex PHP functions together, you might have accidentally created a syntax error. Always run your application locally (or run your unit tests) to verify the code executes flawlessly *before* you type the final git commit.

9. Common Mistakes

  • Forgetting to Add: Beginners will painstakingly edit the file, save it, and immediately type git commit. Git will throw an error saying "Umerged paths". You *must* run git add <file> first. Using git add is how you officially communicate to Git: "I have reviewed this file and the conflict is formally resolved."

10. Exercises

  1. 1. What are the three logical choices a developer must choose between when resolving the code inside a conflict block?
  1. 2. What specific command transitions a file from a "conflicted" state to a "resolved" state in Git's eyes?

11. FAQs

Q: Can I resolve conflicts on GitHub.com instead of in my terminal? A: Yes! If a Pull Request has a minor conflict, GitHub provides a built-in web editor that highlights the conflict markers and allows you to resolve them directly in the browser. However, for complex conflicts spanning multiple files, the local terminal is vastly superior.

12. Summary

In Chapter 5, we conquered the manual resolution process. We learned that resolving conflicts is not a mystical Git command, but a human editorial decision. By executing a strict 4-step workflow—Edit, Save, Stage, and Commit—we successfully eliminated conflict markers and synthesized divergent code. We acknowledged the importance of testing our resolutions, and we armed ourselves with the ultimate safety net:
git merge --abort`, ensuring that we can approach even the most intimidating conflicts without fear of irreversible data loss.

13. Next Chapter Recommendation

Resolving conflicts in a raw text editor is effective, but it is slow and prone to typographical errors. What if we could use a beautiful, graphical interface to do the heavy lifting? Proceed to Chapter 6: Using Git Tools for Conflict Resolution.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·