Stash, Reset & Recovery

Saving work in progress, undoing changes, and recovering lost commits

Stash

bash
# Stash — save work in progress
git stash                          # stash tracked changes
git stash -u                       # include untracked files
git stash save "WIP: login form"   # with description
git stash list                     # see all stashes
git stash pop                      # apply & remove most recent
git stash apply stash@{2}          # apply specific stash (keep it)
git stash drop stash@{0}           # delete specific stash

# Reset — undo commits
git reset --soft HEAD~1            # undo commit, keep changes staged
git reset --mixed HEAD~1           # undo commit, keep changes unstaged (default)
git reset --hard HEAD~1            # undo commit, DELETE all changes ⚠️

# Revert — safe undo (creates new commit)
git revert HEAD                    # undo last commit safely
git revert abc123                  # undo specific commit

# Reflog — recover "lost" commits
git reflog                         # shows ALL recent HEAD movements
git checkout abc123                # recover to that point
git branch recovery abc123         # create branch from lost commit

# Cherry-pick — copy specific commits
git cherry-pick abc123             # apply one commit to current branch
git cherry-pick abc..def           # apply range

Undo Guide

  • Undo unstaged changes: git checkout -- <file> or git restore <file>
  • Undo staged changes: git reset HEAD <file> or git restore --staged <file>
  • Undo last commit (keep changes): git reset --soft HEAD~1
  • Permanent undo last commit: git reset --hard HEAD~1
  • Undo a pushed commit safely: git revert <hash>
  • Recover deleted branch: git reflog → find hash → git branch <name> <hash>

💬 Reset vs Revert — when to use which?

Reset: rewrites history, use on local branches only. Revert: creates a new commit that undoes changes, safe for shared branches. Never reset commits that others have pulled — use revert instead.