← Back to Chapters

Git Reset

? Git Reset

? Quick Overview

The git reset command is a powerful Git tool used to undo changes by moving the HEAD pointer. It can unstage files, remove commits, or discard changes entirely depending on the mode used.

? Key Concepts

  • Rewrites commit history
  • Works on HEAD, staging area, and working directory
  • Three modes: soft, mixed, hard

? Syntax / Theory

? View Code Example
// General syntax for git reset
git reset <option> <commit-hash>

? Code Examples

Soft Reset

? View Code Example
// Undo last commit but keep changes staged
git reset --soft HEAD~1

Mixed Reset

? View Code Example
// Unstage changes but keep them in working directory
git reset --mixed HEAD~1

Hard Reset

? View Code Example
// Completely discard last commit and all local changes
git reset --hard HEAD~1

? Live Output / Explanation

After running a reset command, Git updates the HEAD pointer. Depending on the mode, your staging area and working directory may also be updated or cleared.

? Interactive Example

Visualize HEAD moving backward on the commit graph while files either stay staged, unstaged, or deleted based on reset mode.

? Use Cases

  • Fixing incorrect commits
  • Cleaning local history
  • Undoing staged files

? Extra Tips

  • Prefer revert for public history
  • Soft reset is safest for undoing commits
  • Use git status before reset
  • Avoid reset on shared branches
  • Create a backup branch before hard reset

? Try It Yourself

  • Undo last commit with soft reset
  • Unstage files using mixed reset
  • Clean repo using hard reset