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.
- 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:
<<<<<<< HEAD
const title = "Yadav Academy — Free Tech Courses";
=======
const title = "Yadav Academy — Open Learning Platform";
>>>>>>> feat/update-heroRead those three markers like a divider:
- Between
<<<<<<< HEADand=======is what your current branch (HEAD) has. - Between
=======and>>>>>>> feat/update-herois what the incoming branch has.
Resolving a Conflict in 3 Calm Steps
- Open the file in your editor, pick the line(s) you want to keep (or blend them together), and delete the three marker lines (
<<<<<<<,=======,>>>>>>>). - Save the file and stage it to tell Git "this conflict is resolved":
bash
git add src/lib/site.ts - 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 file | git 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 GitHub | git revert <commit-sha> |
Notice the key difference between reset and revert:
git reset --soft HEAD~1rewrites 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!
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
You ran git commit locally by mistake and want to undo the commit while keeping all your file edits intact. Which command is safest?