Resolve a merge conflict

Description: Both main and feature changed the first line of title.txt. Merge feature into main and solve the conflict by keeping the text “Learn Git Fast”.

Hint:

  • Edit the file to the final text, remove the markers, then git add and git commit.

Solution

Terminal output

$ git merge feature
Auto-merging title.txt
CONFLICT (content): Merge conflict in title.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat title.txt
<<<<<<< HEAD
Learn Git Easily
=======
Learn Git Quickly
>>>>>>> feature
$ echo "Learn Git Fast" > title.txt
$ git add title.txt
$ git commit -m "Merge feature and solve conflict"
[main b4f0e8e] Merge feature and solve conflict
$ git status
On branch main
nothing to commit, working tree clean

How it works

  • Git stops the merge and writes the markers in the file.
  • We replaced the file content with the final text, so no markers are left.
  • git add tells Git the conflict is solved, and the commit finishes the merge.

Read Git Merge Conflict.


Learn more about the similar topics:
Tutorials
Git Branch
Git Checkout and Switch
Git Merge
Git Merge Conflict
Git Rebase
Git Cherry Pick
Git Branching Strategy
Exercises & Assignments
Create a new branch and switch to it
Merge a feature branch into main
Resolve a merge conflict
Delete a merged branch
Rename a local branch
Copy one commit to another branch with cherry-pick
Interview Questions & Answers
How you can see list of all branches in Git?
How do I create a new branch in Git?
How to switch to an another branch in Git?
How to merge one branch to another in Git?
How to rename a local Git branch?
What is the difference between git merge and git rebase?
What is a merge conflict and how do you resolve it?