Git Merge Conflict
A merge conflict happens when two branches change the same lines of the same file, and Git cannot decide which version to keep. Do not be afraid of it. It is normal in team work, and the fix is easy when you know the steps.
When does a conflict happen?
- Two branches edit the same line of a file in different ways.
- One branch deletes a file and the other branch edits it.
- Both branches add a file with the same name but different content.
Create a conflict
In this example, the main branch and the feature branch both change the title line of the same file.
$ git switch -c feature
Switched to a new branch 'feature'
$ printf "Title: Welcome to our shop\nLine 2\n" > page.txt
$ git commit -q -am "Change title in feature"
$ git switch main
Switched to branch 'main'
$ printf "Title: Welcome to Tutorials Class\nLine 2\n" > page.txt
$ git commit -q -am "Change title in main"
$ git merge feature
Auto-merging page.txt
CONFLICT (content): Merge conflict in page.txt
Automatic merge failed; fix conflicts and then commit the result.
Check the status
$ git status
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge –abort" to abort the merge)
Unmerged paths:
(use "git add <file>…" to mark resolution)
both modified: page.txt
no changes added to commit (use "git add" and/or "git commit -a")
The file is shown under “Unmerged paths”, with both sides modified.
Look inside the file
$ cat page.txt
<<<<<<< HEAD
Title: Welcome to Tutorials Class
=======
Title: Welcome to our shop
>>>>>>> feature
Line 2
Git added conflict markers to the file:
| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Start of your current branch version (main) |
======= | Separator between the two versions |
>>>>>>> feature | End of the version from the other branch (feature) |
Resolve the conflict
Open the file in your editor, decide what the final text should be, and remove all the three marker lines. You can keep one side, or the other side, or write a mix of both. Here we write a new title.
$ printf "Title: Welcome to our Tutorials Class shop\nLine 2\n" > page.txt
$ cat page.txt
Title: Welcome to our Tutorials Class shop
Line 2
Mark it as resolved and finish the merge
$ git add page.txt
$ git commit -m "Merge feature and solve title conflict"
[main 1924946] Merge feature and solve title conflict
$ git log –oneline –graph
* 1924946 (HEAD -> main) Merge feature and solve title conflict
|\
| * d0ab721 (feature) Change title in feature
* | 2f825e8 Change title in main
|/
* e7ecea8 Add page
Quick options to choose one side
If you want to take a full file from one side and skip the manual editing:
git checkout --ours page.txt # keep the version of the current branch
git checkout --theirs page.txt # keep the version of the merged branch
Cancel the merge
git merge --abort
Tips to avoid conflicts
- Pull the latest changes of main often, and merge it into your branch.
- Keep branches short, and make small commits.
- Talk to your team when two people must change the same file.
- Do not reformat a full file together with a small change.
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |