Git and GitHub · Part 2 · Branches and merging

Merge conflicts

What a conflict is

Git combines branches on its own when they changed different files or different lines. When both changed the same lines, it can't know which version is right. It won't guess; it stops and asks.

That's all a merge conflict is: Git asking a person to decide. Nothing is broken, and it happens on every team, every day.

What the branches didConflict?
Changed different filesNo, merged automatically
Changed different lines of the same fileNo, merged automatically
Changed the same linesYes
One edited a file, the other deleted itYes

Making a conflict on purpose

Terminal
cd ~/Desktop
mkdir conflict-lab
cd conflict-lab
git init

cat > hero.html << 'EOF'
<h1>Welcome to Our Store</h1>
<p>We open at 9 AM</p>
<button>Shop Now</button>
EOF
git add hero.html
git commit -m "Add hero section"

cat > file << 'EOF' writes several lines into a file at once. Now change the same line on two branches:

Terminal
git switch -c feature/early-hours
sed -i '' 's/9 AM/8 AM/' hero.html
git commit -am "Change opening time to 8 AM"

git switch main
sed -i '' 's/9 AM/10 AM/' hero.html
git commit -am "Change opening time to 10 AM"

git merge feature/early-hours
Output
Auto-merging hero.html
CONFLICT (content): Merge conflict in hero.html
Automatic merge failed; fix conflicts and then commit the result.

sed replaces text in a file (-i '' is the Mac syntax); editing the file in VS Code works just as well.

Resolving it, step by step

1.Ask Git what's going on

Terminal
git status
Output
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:   hero.html

A merge is in progress, hero.html is the conflicted file, and Git says how to finish (git add, then git commit) or back out (git merge --abort).

2.Read the conflict markers

hero.html
<h1>Welcome to Our Store</h1>
<<<<<<< HEAD
<p>We open at 10 AM</p>
=======
<p>We open at 8 AM</p>
>>>>>>> feature/early-hours
<button>Shop Now</button>
MarkerMeaning
<<<<<<< HEADThe current branch's version starts (“ours” or “current”)
=======The divider between the two versions
>>>>>>> feature/early-hoursThe incoming branch's version ends (“theirs” or “incoming”)

Everything outside the markers, like the <h1> and the button, merged without a problem.

3.Decide, and edit the file

There are four ways to resolve it:

OptionResult
Keep ours (main)<p>We open at 10 AM</p>
Keep theirs (feature)<p>We open at 8 AM</p>
Keep bothBoth lines, one after the other: often right in code, such as two different imports
Write something new<p>We open at 8 AM on weekdays, 10 AM on weekends</p>

Here, write something new. Open hero.html, make it look like this, and save:

hero.html
<h1>Welcome to Our Store</h1>
<p>We open at 8 AM on weekdays, 10 AM on weekends</p>
<button>Shop Now</button>

4.Mark it as resolved

Terminal
git add hero.html

During a conflict, git add means “this file is fixed.” git status confirms it:

Output
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

5.Finish the merge

Terminal
git commit --no-edit
git log --oneline --graph
Output
*   50bd060 (HEAD -> main) Merge branch 'feature/early-hours'
|\
| * 19ce9b1 (feature/early-hours) Change opening time to 8 AM
* | 0a7e0fe Change opening time to 10 AM
|/
* 736b8ae Add hero section

The conflict is resolved. (git merge --continue does the same as the commit.)

The whole process

1. git merge branch     → a conflict appears
2. git status           → see which files are conflicted
3. edit each file       → pick, combine or rewrite, and delete the markers
4. git add file         → mark it resolved
5. git commit           → finish the merge

At any point, git merge --abort puts everything back to how it was before the merge. Trying can't break anything.

Resolving in VS Code

VS Code highlights each conflict and puts buttons above it:

Accept Current ChangeAccept Incoming ChangeAccept Both ChangesCompare Changes

<<<<<<< HEAD (Current Change)

<p>We open at 10 AM</p>

=======

<p>We open at 8 AM</p>

>>>>>>> feature/early-hours (Incoming Change)

ButtonDoes
Accept Current ChangeKeeps ours
Accept Incoming ChangeKeeps theirs
Accept Both ChangesKeeps both
Compare ChangesShows the two side by side
  • The buttons remove the markers too. To write something new, click one, then edit the text.
  • For big conflicts, Resolve in Merge Editor (bottom right) shows ours, theirs and the result in separate panels.
  • The Source Control panel lists conflicted files under Merge Changes. Its + button is the same as git add.

Taking one side for a whole file

When one side's version of a file is the right one, for example because the feature rewrote it, take it whole:

Terminal
git checkout --ours hero.html     # the current branch's version
git checkout --theirs hero.html   # the incoming branch's version
git add hero.html

The file becomes that side's version, with no markers. git restore --ours and --theirs do the same.

Other kinds of conflicts

1.Several conflicts or files

A file can have several conflict blocks; search for <<<<<<< to find each one. git status lists every conflicted file. Fix and git add each one, then commit once.

2.Edited on one side, deleted on the other

Output
CONFLICT (modify/delete): about.html deleted in feature/cleanup and modified in HEAD.

To keep the file, git add about.html. To delete it, git rm about.html. Then commit.

3.package-lock.json

Lock file conflicts are common on JavaScript projects and run to hundreds of lines. Don't fix them by hand: resolve package.json first, then take either side of the lock file and let npm rebuild it:

Terminal
git checkout --theirs package-lock.json
npm install
git add package-lock.json

4.Not only during merges

git revert, git rebase, git stash pop and git cherry-pick can stop with conflicts too. The fix is the same: edit, remove the markers, git add, then --continue (for example git rebase --continue), or --abort to back out.

Checking before committing

To catch a forgotten marker, run this before git add:

Terminal
git diff --check
Output, if a marker is left
hero.html:2: leftover conflict marker
hero.html:4: leftover conflict marker
hero.html:6: leftover conflict marker

No output means it's clean. Once a file is staged, plain git diff --check no longer sees it; use git diff --cached --check instead.

Getting fewer conflicts

HabitWhy it helps
Merge main into the branch oftenSmall conflicts now instead of big ones later
Keep branches short, days rather than weeksLess time to drift apart
Make small, atomic commitsConflicts are smaller and easier to read
Talk to the team“I'm rewriting the navbar this week” stops two people editing the same code
Use a formatter like PrettierNo conflicts over spacing or quote style

Quick reference

To…Run
See which files are conflictedgit status
Mark a file as resolvedgit add file
Finish the mergegit commit or git merge --continue
Cancel the mergegit merge --abort
Take the current branch's versiongit checkout --ours file
Take the incoming versiongit checkout --theirs file
Accept a deletiongit rm file
Find leftover markersgit diff --check (before staging)