Skip to content

Git Reset & Clean Cheat Sheet ​

Reset modes ​

ModeHEADIndexWorking tree
--softMovesUnchangedUnchanged
--mixed (default)MovesReset to HEADUnchanged
--hardMovesReset to HEADReset to HEAD

Common one-liners ​

bash
# Unstage everything (keep edits)
git reset

# Unstage a specific file
git reset -- main.go

# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Undo last 3 commits, unstage
git reset HEAD~3

# Discard all local changes
git reset --hard HEAD

# Discard all local changes + untracked files
git reset --hard HEAD && git clean -fd

# Discard everything including gitignored
git reset --hard HEAD && git clean -fdx

# Squash last N commits
git reset --soft HEAD~3 && git commit -m "feat: combined"

# Restore file to another commit's state (index only)
git reset v2.3.0 -- config.json

# Rollback a failed rebase
git reset --hard ORIG_HEAD

Git clean variants ​

bash
# Preview what would be removed
git clean -n

# Remove untracked files only
git clean -f

# Remove untracked files + directories
git clean -fd

# Remove everything (including gitignored)
git clean -fdx

# Remove only gitignored files
git clean -fX

CI patterns ​

bash
# Pristine checkout before build
git reset --hard HEAD && git clean -fdx

# Preflight dirty-tree assertion
git diff --quiet || { echo "dirty"; exit 1; }

# Safe reset with auto-stash insurance
git stash push -m "pre-reset $(date)"
git reset --hard HEAD

# Selective unstage
git reset -- 'dist/*' '*.min.js'

# Matrix worktree cleanup
git -C "$WT_PATH" reset --hard HEAD
git -C "$WT_PATH" clean -fdx

Recovery ​

bash
# Restore after too-aggressive reset
git reset --hard ORIG_HEAD

# Restore via reflog
git reflog
git reset --hard HEAD@{3}

Key config ​

bash
# Warn before clean (default: true — prevents bare `git clean`)
git config clean.requireForce true

Git Master — Git Reference for Every Skill Level