Git ‘oh no’ moments: quick undo recipes that actually work

June 10, 2025 (3mo ago)

We all break branches. Here are the commands I reach for the most.

I committed to the wrong branch

git switch correct-branch
git cherry-pick <bad-commit-sha>
git switch -
git reset --hard HEAD~1

I nuked local changes by accident

git reflog
# find the SHA before the reset, then
git reset --hard <sha>

I need to stash only some files

git stash push -m "wip: partial" -- path/to/fileA path/to/fileB
git stash list
git stash apply stash@{0}

I want to undo the last commit but keep changes staged

git reset --soft HEAD~1

I force‑pushed the wrong thing

git reflog origin/main
git push origin <sha>:main --force-with-lease

Keep reflog in your muscle memory—it’s the time machine.