Course contentsShow
← Git & GitHub
Lesson 45 of 963. Branching and TeamworkFree lesson

Manually Resolving Code Conflicts

Learners will edit files to remove conflict markers and select the correct code logic to finalize a successful merge operation.

In this lesson, you will learn how to manually edit your source code to resolve conflicting changes and finalize a successful merge.

When Git encounters a merge conflict, it stops the process and pauses at the state where the code diverges. It inserts conflict markers into the affected files to show you exactly where the two branches disagree. Your job is to act as the "final judge" by manually choosing which code to keep, which to discard, or how to blend both logic paths together.

Identifying the Conflict Zone

When you open a conflicted file, you will see markers that look like this:

<<<<<<< HEAD
print("Hello from the main branch!")
=======
print("Hello from the feature branch!")
>>>>>>> feature-branch-name
  • <<<<<<< HEAD: The start of your current branch’s changes.
  • =======: The divider between the two competing versions.
  • >>>>>>> feature-branch-name: The end of the incoming changes from the branch you are trying to merge.

Resolving the Conflict

To resolve this, you must edit the file in your text editor:

  1. Delete the markers: Remove the <<<<<<<, =======, and >>>>>>> lines entirely.
  2. Edit the code: Modify the text inside to represent the logic you actually want. You might choose the code from HEAD, the code from the feature branch, or a combination of both that satisfies your program's needs.
  3. Save the file: Once the file looks exactly how you want it to appear in the final merge, save your changes.

After you have manually cleaned up all conflicted files, you must stage them to tell Git the conflict is resolved:

git add <filename>

Once all files are staged, complete the merge by running git commit. Git will automatically generate a merge commit message for you, concluding the process. You have successfully navigated the conflict and integrated your work!

Key Takeaway: Manually resolving a conflict is the process of removing Git’s markers and editing the code to reflect your final, desired logic before staging and committing the resolution.