Git & GitHub Course
Git & GitHub
/
Advanced

Soft vs Mixed vs Hard Reset

Definition

The three modes of `git reset` which determine what happens to your files in the Working Directory and Staging Area when you move the timeline backwards.

Explain Like I'm New

If you reset 1 commit backward: - `--soft`: Keeps your code exactly as it is, and leaves it in the Staging Area ready to be committed again. - `--mixed` (Default): Keeps your code exactly as it is, but pulls it out of the Staging Area. - `--hard`: DELETES your code completely. Wipes the files back to how they looked in the old commit.

Real World Example

You made 5 tiny commits ('typo', 'fix', 'oops'). You use `git reset --soft HEAD~5` to rip out those 5 commits, keeping all the code perfectly intact on the stage, so you can bundle it into one single, clean commit.

Common Use Cases

  • •Squashing commits locally
  • •Completely nuking experimental code (--hard)

Terminal Output

bash / terminal
# 1. --SOFT: Undo the commit, but keep files Staged # Great for editing the commit message or adding one more file to the commit $ git reset --soft HEAD~1 # 2. --MIXED (Default): Undo commit, and Unstage files # Great if you realized you need to rewrite the code completely before staging $ git reset HEAD~1 # 3. --HARD: Destroy the commit, Destroy the Staging Area, Destroy the Working Directory # Great for completely abandoning a terrible idea and wiping the slate clean $ git reset --hard HEAD~1

Interview Questions

basic

  • Which reset mode is the most dangerous and can permanently delete code?

intermediate

  • If you don't provide a flag (just `git reset HEAD~1`), which mode is used?

Flash Cards

Question

Most dangerous?

Click to reveal answer
Answer

`--hard`. It forcefully overwrites your Working Directory. Any uncommitted code is destroyed.

Question

Default mode?

Click to reveal answer
Answer

`--mixed`. It moves the commit pointer back, and unstages the files, but leaves the code safely in your text editor.