Merge conflicts look scary, but they follow a simple pattern. In this lesson you will create a conflict on purpose, read the conflict markers, resolve it on the command line and in VS Code, handle special cases and learn habits that prevent painful conflicts.
๐ What You’ll Learn: what causes conflicts, creating a practice conflict, finding conflicted files, reading conflict markers, resolving and finishing a merge, VS Code conflict tools, –ours and –theirs, aborting, other conflict types, and preventing conflicts.
๐งฏ What Is a Merge Conflict?
A merge conflict happens when two branches changed the same lines of the same file (or one branch edited a file the other deleted) and Git cannot decide which version to keep. Git pauses and asks you to decide.
Conflicts are normal in teamwork. They are not errors, and nothing is lost: both versions are still there.
Conflicts can appear during git merge, git pull, git rebase, git cherry-pick and git stash pop.
๐งช Create a Conflict to Practise
echo "timeout: 10000" > config.txt
git add config.txt
git commit -m "Add config file"
git switch -c feature/timeout
echo "timeout: 30000" > config.txt
git commit -am "Set timeout to 30 seconds"
git switch main
echo "timeout: 60000" > config.txt
git commit -am "Set timeout to 60 seconds"
git merge feature/timeout
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.
๐ Step 1: See Which Files Conflict
git status
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: config.txt
๐งฉ Step 2: Read the Conflict Markers
Open the file. Git has inserted conflict markers:
<<<<<<< HEAD
timeout: 60000
=======
timeout: 30000
>>>>>>> feature/timeout
| Marker | Meaning |
|---|---|
<<<<<<< HEAD | Start of your current branch’s version (here: main) |
======= | Separator |
>>>>>>> feature/timeout | End of the incoming branch’s version |
โ๏ธ Step 3: Decide and Edit
Choose one side, the other, or a combination, and delete all three marker lines:
timeout: 45000
๐ก Tip: Do not resolve blindly. Read both versions, understand why each change was made (check git log of each branch), and ask the teammate if unsure. For test code, run the affected tests after resolving.
โ Step 4: Mark as Resolved and Finish
git add config.txt # tells Git the conflict in this file is resolved
git status # "All conflicts fixed but you are still merging."
git commit # completes the merge (the message is pre-filled)
For a rebase the finish command is git rebase --continue; for cherry-pick, git cherry-pick --continue.
๐งโ๐ป Resolving in VS Code
VS Code highlights conflicts and shows clickable actions above each one:
- Accept Current Change: keep your branch’s version (HEAD).
- Accept Incoming Change: keep the other branch’s version.
- Accept Both Changes: keep both, one after the other.
- Compare Changes: side-by-side view.
You can also open the 3-way Merge Editor (Resolve in Merge Editor) to see “Incoming”, “Current” and the “Result” at once. After saving, stage the file in the Source Control panel and commit. See Git in VS Code.
โก Take a Whole File from One Side
git checkout --ours config.txt # keep the current branch's version
git checkout --theirs config.txt # keep the incoming branch's version
git add config.txt
๐ Note: During a rebase, “ours” and “theirs” are swapped: “ours” is the branch you are rebasing onto, “theirs” is your own commit being replayed.
โฉ๏ธ Not Ready? Abort
git merge --abort # back to the state before the merge
git rebase --abort
git cherry-pick --abort
๐งท Other Conflict Types
| Type | Shown as | How to resolve |
|---|---|---|
| Both modified | both modified | Edit markers as above |
| Modify/delete | deleted by them or deleted by us | Keep the file (git add file) or delete it (git rm file) |
| Both added | both added | Combine or choose one version |
| Binary files (images, PDFs) | No markers | Choose with --ours or --theirs |
๐ก๏ธ How to Avoid Painful Conflicts
- Pull or merge
maininto your branch often, so differences stay small. - Keep branches short-lived and pull requests small.
- Communicate when you refactor shared files.
- Use a formatter (for example Prettier) so formatting changes do not collide.
- Avoid committing generated files (reports, lock files from different tools) by using .gitignore.
๐ Assignment
- Recreate the timeout conflict from this lesson and resolve it by hand, then finish the merge.
- Create the same conflict again and abort it with
git merge --abort. - Create a conflict and resolve it in VS Code with “Accept Both Changes”, then clean up the result.
- Create a modify/delete conflict (one branch edits a file, the other deletes it) and resolve it both ways.
- Bonus: create a conflict during
git rebaseand resolve it withgit rebase --continue.
โ FAQ
๐ค How do I resolve a merge conflict in Git?
Run git status to find the files, edit each file to the correct content and remove the <<<<<<<, ======= and >>>>>>> markers, then git add the files and git commit.
๐ค What do <<<<<<< HEAD and >>>>>>> mean?
They mark a conflict. The part after <<<<<<< HEAD is your current branch’s version; the part before >>>>>>> branch is the incoming version, separated by =======.
๐ค How do I cancel a merge with conflicts?
git merge --abort returns your branch and files to the state before the merge started.
๐ค How do I accept all incoming changes for a file?
git checkout --theirs file during a merge, then git add file.
๐ค Why do merge conflicts happen?
Two branches changed the same lines of a file, or one changed a file that the other deleted, so Git cannot combine them automatically.
๐ฏ Next Steps
- ๐งฌ git rebase and Rebase vs Merge
- ๐ฆ git stash: Save Work in Progress
- ๐ Git and GitHub Tutorial: full course
๐ฌ Learn Git and GitHub step by step: watch the series on YouTube!
Subscribe to SqaToolsByDY • Practice on sqatools.in
4 thoughts on “How to Resolve Merge Conflicts in Git (Step by Step with VS Code)”