Chapter 37: Git Reset

Git reset

This is the command that makes people say:

  • “Git is amazing — I can undo almost anything!”
  • …and five minutes later: “Oh no — I just lost three days of work — help!”

So today we are going to learn git reset properly — slowly, carefully, with real examples, clear mental models, exact outputs you will see, and most importantly: when to use each flavor and when NEVER to use it.

1. What does git reset actually do? (very simple explanation)

git reset moves the branch pointer (HEAD + current branch) to a different commit

It is not creating a new commit (like revert). It is rewriting where your branch is pointing — and depending on the options, it can also change (or throw away) your working directory and staging area.

Think of your branch as a movable label stuck to one commit in the timeline.

  • git commit → moves the label forward to the new commit
  • git reset → moves the label backward (or sideways) to an older (or different) commit

Three very different behaviors depending on the –flag you use:

Flag What happens to the commit history What happens to staging area (index) What happens to working directory files Safety level Most common use case
–soft Stays exactly the same Keeps all changes staged Keeps all changes Very safe Undo commit but keep changes to re-commit better
–mixed (default) Stays exactly the same Removes everything from staging Keeps all changes (unstaged) Safe Most common “undo commit” locally
–hard Stays exactly the same Throws away staging Throws away all changes Dangerous Reset everything to match a previous commit

Very important sentence to remember forever:

git reset never deletes commits from the repository — it only moves the branch pointer. The old commits are still there (until garbage collection ~90 days later) — you can recover them with git reflog.

2. Realistic Example – Let’s See All Three Flavors

Create a small playground repo so you can follow along:

Bash

Now history:

Bash

Example 1 – git reset –soft (undo commit but keep changes staged)

You realize the last commit message was bad and you forgot one line.

Undo last commit but keep changes ready to re-commit:

Bash

Now:

Bash
text
Bash

→ You can now:

Bash

→ Clean new commit instead of ugly one.

Example 2 – git reset –mixed (default – most common local undo)

You want to unstage the last commit’s changes but keep them in working directory:

Bash

Now:

Bash
text
Bash

→ Perfect when you want to reorganize commits or throw away only the commit, not the code.

Example 3 – git reset –hard (dangerous – full reset)

You want to completely throw away the last two commits and their changes:

Bash

Now:

Bash
text
Bash

→ The last two commits disappeared from your working tree and staging → But they are not gone forever yet — see reflog below

Warning: –hard is irreversible unless you act fast (reflog).

3. Recovering from “I did git reset –hard and panicked”

Git keeps a private log of where HEAD has been (reflog):

Bash

Typical output:

text

To go back to where you were:

Bash

→ Everything restored!

Reflog usually keeps entries ~90 days — your safety net.

4. Quick git reset Cheat Sheet (save forever)

What you want to achieve Command Safety Typical use case
Undo last commit – keep changes staged git reset –soft HEAD~1 Very safe Fix bad message / add forgotten file
Undo last commit – keep changes unstaged git reset –mixed HEAD~1 Safe Most common local “undo commit”
Throw away last commit + changes git reset –hard HEAD~1 Dangerous Local cleanup – only when you are sure
Go back 3 commits – keep changes git reset –mixed HEAD~3 Safe Reorganize recent history
Recover after bad reset git reflog → git reset –hard <hash> Safe Your personal undo history

Got the reset feeling now?

git reset = “move my branch pointer to another commit — and optionally throw away or keep my working changes”

It is very powerful locally, very dangerous on shared branches.

Next?

  • Want to practice a full “bad commit → reset –mixed → re-commit clean” flow?
  • See difference when you reset after push?
  • Or move to rebase vs reset vs revert comparison?

Just tell me — we’ll continue step by step. You’re getting really strong with Git — proud of you! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *