← Back to Git Course | Chapter 5: Undoing Changes | Lesson 1 of 6

Git reset Command

git reset is like rewinding a video to an earlier scene so the later scenes are no longer part of your story on that branch.
Syntax
bash
git reset --soft commit_reference
git reset [--mixed] commit_reference
git reset --hard commit_reference

What is Git Reset?

git reset rewinds your current branch to point at an earlier commit, effectively undoing the commits after that point as far as the branch history is concerned. Unlike git revert, it changes history rather than adding a new commit on top, which is powerful but riskier on shared branches.

Example: What is Git Reset?

bash
git reset a1b2c3d

Soft Reset

--soft moves only the branch pointer back — your staged changes and working directory files stay exactly as they were, so everything from the "undone" commits reappears as staged changes ready to be re-committed differently. This is the safest reset mode and a common way to squash several small commits into one.

Example: Soft Reset

bash
git reset --soft HEAD~1

Mixed Reset (Default)

The default mode (no flag, or --mixed) moves the branch pointer back and also empties the staging area, but leaves your working directory files untouched — so your edits survive, they're just no longer staged. Use this when you want to re-review and re-stage changes before committing again.

Example: Mixed Reset (Default)

bash
git reset HEAD~1
git reset --mixed HEAD~1

Hard Reset (Dangerous)

--hard moves the branch pointer back AND overwrites both the staging area and your working directory to match that commit exactly, permanently discarding any uncommitted work in the process. There's no confirmation prompt, so double-check you actually want to lose those changes before running it.

Example: Hard Reset (Dangerous)

bash
git reset --hard HEAD~1

Recovering from Reset

Even after a hard reset, the commits you "lost" usually aren't gone — Git's reflog keeps a record of where your branch tip has pointed recently, so you can look up the old commit hash and reset back onto it. This safety net is why hard resets, while scary, are rarely truly catastrophic within the reflog's retention window.

Example: Recovering from Reset

bash
git reflog
git reset --hard a1b2c3d
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Running git reset --hard HEAD~1 and losing uncommitted changes with no way to get them back through the working tree.
  2. Using git reset on commits that were already pushed, which rewrites shared history.
  3. Confusing the modes, expecting --mixed to keep files staged, when --soft keeps them staged and --mixed (default) unstages them.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.