2. Git Core Concepts

Merge Conflicts & Safely Undoing Mistakes

Stop panicking at <<<<<<< markers: resolve Git merge conflicts calmly step by step and undo local or pushed mistakes safely with git restore, reset, and revert.

2 min readUpdated 2026-11-29
In This Lesson
  • Read Git conflict markers (<<<<<<<, =======, >>>>>>>) and resolve them cleanly
  • Discard local edits or unstage files safely using git restore
  • Choose between git reset --soft (local) and git revert (pushed commits)

Even experienced engineering teams hit two moments in Git every week: merge conflicts and committing too early. Neither is scary once you see what Git is actually asking you to do.

Why Merge Conflicts Happen

Usually, Git merges branches automatically without bothering you—for example, when Branch A edits Header.tsx and Branch B edits Footer.tsx.

A merge conflict only happens when both branches edit the exact same lines of the exact same file. Git refuses to guess which person's edit should win, so it pauses the merge and puts both versions side by side inside the file:

text
<<<<<<< HEAD
const title = "Yadav Academy — Free Tech Courses";
=======
const title = "Yadav Academy — Open Learning Platform";
>>>>>>> feat/update-hero

Read those three markers like a divider:

  • Between <<<<<<< HEAD and ======= is what your current branch (HEAD) has.
  • Between ======= and >>>>>>> feat/update-hero is what the incoming branch has.

Resolving a Conflict in 3 Calm Steps

  1. Open the file in your editor, pick the line(s) you want to keep (or blend them together), and delete the three marker lines (<<<<<<<, =======, >>>>>>>).
  2. Save the file and stage it to tell Git "this conflict is resolved":
    bash
    git add src/lib/site.ts
  3. Complete the merge commit:
    bash
    git commit

Safely Undoing Common Mistakes

Every mistake in Git has a clean, predictable undo command depending on how far the change has gone:

What Happened?Safe Command
Discard unsaved local edits in one filegit restore path/to/file.ts
Unstage a file you added by mistake (keeps your edits!)git restore --staged path/to/file.ts
Undo your last local commit (keeps all your edits!)git reset --soft HEAD~1
Undo a commit already pushed to GitHubgit revert <commit-sha>

Notice the key difference between reset and revert:

  • git reset --soft HEAD~1 rewrites local history (great before pushing to GitHub).
  • git revert <sha> leaves history intact and creates a brand-new opposite commit—so teammates pulling from GitHub never get out of sync!
Avoid git reset --hard Unless You Mean It

git reset --hard permanently erases uncommitted work without saving a copy! Prefer git reset --soft HEAD~1 or git stash whenever you want to keep your changes safe.

Quick Checkpoints

Check Your Understanding

You ran git commit locally by mistake and want to undo the commit while keeping all your file edits intact. Which command is safest?