# Undoing Commits: Reset

> How `git reset` rewinds history, and the critical differences between `--soft`, `--mixed`, and `--hard`.

Subject: Version Control | Lesson 20 of 29 | 6 min read
Interactive lesson: https://codewithanimation.in/git/git-reset
Previous: [Safely Undoing Commits](https://codewithanimation.in/git/git-revert.md) | Next: [The Reflog](https://codewithanimation.in/git/git-reflog.md)

## The git reset command

`git reset` is a powerful command that rewinds your branch's history to a specific past commit.

Unlike `git revert`, which adds a new commit to undo changes, `reset` actively erases recent commits from your local history by pointing `HEAD` backward.

## Why it matters

During development, it's common to make messy, experimental WIP (Work In Progress) commits.

`git reset` allows you to clean up your local history before pushing, combining messy commits into a single cohesive one (squashing) so your teammates only see clean, logical work.

## The Three Modes

`git reset` operates on the 3 Trees (History, Staging, Working Directory).

**`--soft`**: Moves `HEAD` back, but leaves Staging and Working Directory untouched. Changes are ready to be re-committed.

**`--mixed`** (Default): Moves `HEAD` back and empties the Staging area. Changes exist in your Working Directory, but are unstaged.

**`--hard`**: Moves `HEAD` back, empties Staging, AND overwrites the Working Directory. All uncommitted work is destroyed.

## Common mistake: Rewriting Public History

The biggest danger of `git reset` is using it on commits that have already been pushed to a remote shared branch.

Because it erases history, if you force-push a reset, you will cause massive synchronization nightmares for any teammates who have already pulled the original commits.

## Tip: The Three Buckets

Mental Model: Think of Git as three vertical buckets (History on top, Staging middle, Working Directory on bottom).

`--soft` pours History into Staging. `--mixed` pours it all the way down into the Working Directory. `--hard` dumps Staging and Working Directory straight into the trash.

## Try it: Compare the three reset modes

Inspect how `--soft`, `--mixed` and `--hard` affect HEAD, the index and the working tree.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-reset

## Check yourself

### Question 1

You made three messy commits while trying to center a div. You want to combine them into one clean commit, but you don't want to lose your code changes. Which command should you use?

- A. `git reset --hard HEAD~3`
- B. `git revert HEAD~3`
- C. `git reset --soft HEAD~3`
- D. `git checkout HEAD~3`

Correct answer: C

`--soft` moves the HEAD pointer back by 3 commits but keeps all your actual file changes safely staged, allowing you to instantly make a single, clean commit.

- Why not A: This will permanently delete all the code changes.
- Why not B: This undoes a specific old commit, it doesn't squash.
- Why not D: This puts you in a detached HEAD state.

### Question 2

What is the default behavior of `git reset` if you don't specify a flag like `--soft` or `--hard`?

- A. It performs a `--soft` reset.
- B. It performs a `--mixed` reset.
- C. It performs a `--hard` reset.
- D. It prompts you to choose a mode.

Correct answer: B

By default, `git reset` uses the `--mixed` flag. This safely moves HEAD backward and unstages your changes, but keeps your working files completely intact.

- Why not A: It unstages changes, unlike soft.
- Why not C: Git protects your working directory by default.
- Why not D: It assumes mixed automatically.

### Question 3

Why is it considered dangerous to use `git reset` on commits that have already been pushed to a remote, shared branch?

- A. It permanently blocks you from ever pushing to that remote again.
- B. It automatically deletes the remote repository.
- C. It rewrites history, causing synchronization nightmares for teammates who have already pulled.
- D. It corrupts your local Git installation.

Correct answer: C

`git reset` rewrites history. If you rewrite history that others have based their work on, their local repos will conflict with the new remote history, leading to messy merge conflicts.

- Why not A: You can push with `--force`, which causes the problem.
- Why not B: It won't delete the repository itself.
- Why not D: Reset works perfectly fine locally.

## Remember this

- `--soft`: Uncommits, keeps changes staged.
- `--mixed` (default): Uncommits, keeps changes unstaged.
- `--hard`: Uncommits, destroys all changes. Use with extreme caution.
- Never reset public, pushed commits!
