How to Resolve Merge Conflicts in Git (Step by Step with VS Code)

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.

Advertisement

๐Ÿ“– 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
MarkerMeaning
<<<<<<< HEADStart of your current branch’s version (here: main)
=======Separator
>>>>>>> feature/timeoutEnd 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

TypeShown asHow to resolve
Both modifiedboth modifiedEdit markers as above
Modify/deletedeleted by them or deleted by usKeep the file (git add file) or delete it (git rm file)
Both addedboth addedCombine or choose one version
Binary files (images, PDFs)No markersChoose with --ours or --theirs

๐Ÿ›ก๏ธ How to Avoid Painful Conflicts

  • Pull or merge main into 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

  1. Recreate the timeout conflict from this lesson and resolve it by hand, then finish the merge.
  2. Create the same conflict again and abort it with git merge --abort.
  3. Create a conflict and resolve it in VS Code with “Accept Both Changes”, then clean up the result.
  4. Create a modify/delete conflict (one branch edits a file, the other deletes it) and resolve it both ways.
  5. Bonus: create a conflict during git rebase and resolve it with git 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

๐ŸŽฌ 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)”

Leave a Comment