Rewriting History

rebase, reset, revert, stash, cherry-pick and reflog without fear: a readable history and no “lost” commits.

4 lessons8 interview questions4 terminal practices

reset --hard on the wrong commit, and the work is "gone". It isn't. Git keeps it in reflog. This module turns the scariest commands into everyday tools: you clean up "wip" and "fix typo" before review, undo a bad release without breaking the team, and bring "lost" commits back.

CourseGit Basics Course · Module 6 of 7 · Stage 2 "In a team"
Learnrebase and interactive rebase, reset vs revert, stash, cherry-pick, reflog, rules for shared branches
How4 lessons on my-plugin with three practice branches: feature/coupons, feature/debug, release/1.2

Pick the right undo

flowchart TD
  Q{"Has the commit<br/>been pushed and shared?"}
  Q -- "No, only on my machine" --> L["Rewrite it<br/>reset · rebase -i · --amend"]
  Q -- "Yes, others have it" --> S["Add a new commit<br/>git revert"]
  L --> X{"Went wrong?"}
  X -- "yes" --> RF(["git reflog → back in seconds"])
  classDef q fill:#fff7ed,stroke:#EE5133,color:#7c2d12
  classDef safe fill:#ecfdf5,stroke:#10b981,color:#065f46
  classDef rescue fill:#EE5133,stroke:#9a2a14,color:#ffffff
  class Q,X q
  class L,S safe
  class RF rescue

Six tools, one table

ToolUse it whenNever
git rebase -isquash "wip" commits before a PRon a branch others pulled
git resetundo local commitson shared main
git revertroll back a bad release in main— it's always safe
git stashan emergency hits mid-featureas long-term storage
git cherry-pickone fix needed on release/1.2instead of a proper merge
git refloga commit "disappeared"— it's your safety net

You'll be able to answer

  • What's the difference between merge and rebase?
  • What's the difference between reset and revert?
  • How do you recover a commit after git reset --hard?
  • Which operations must not be done on shared branches?

Lessons in this module 4 lessons