Git & GitHub ยท Chapter 20 of 42

git reset (soft, mixed, hard)

`git reset` moves the current branch pointer to a different commit, and can also change the staging area and working directory depending on the mode used. It's a powerful tool for undoing commits.

The three modes are: `--soft` (keeps changes staged), `--mixed` (default, unstages changes but keeps them in the working directory), and `--hard` (discards changes completely).

Syntax
git reset --soft <commit>
git reset --mixed <commit>
git reset --hard <commit>

Soft and mixed reset

`git reset --soft HEAD~1` undoes the last commit but keeps changes staged. `git reset --mixed HEAD~1` (the default) undoes the commit and unstages changes, but keeps them in your files.

Hard reset (dangerous)

`git reset --hard HEAD~1` undoes the commit and permanently discards all related changes from the working directory. Use with extreme caution.

Example 1 (bash)
git reset --soft HEAD~1

Undoes the last commit but keeps its changes staged, ready to be re-committed.

Example 2 (bash)
git reset --hard HEAD~1
Output
HEAD is now at a3f5c9e Add login validation

Undoes the last commit and discards all its changes completely from the working directory.

Key points

  • git reset moves the branch pointer to a different commit.
  • --soft keeps changes staged after the reset.
  • --mixed (default) unstages but keeps changes in your files.
  • --hard permanently discards changes โ€” use with caution.
๐Ÿ’ก Note: Never use git reset --hard on commits already shared with others, since it rewrites history they may depend on.

๐Ÿ“ Quick Quiz

1. Which reset mode keeps changes staged?

2. Which reset mode permanently discards changes?

3. What is the default mode of git reset?