Skip to main content
⚡ OpenDevDocs
Git
gitgitGuideBeginner

Undoing Mistakes in Git

Comprehensive emergency guide for undoing Git mistakes: uncommitted edits, accidental commits, wrong branch commits, and recovering deleted files.

Verified: 2026-09-30
Platforms:
all
#git#undo#troubleshooting#restore#reset

Undoing Mistakes in Git

A practical emergency decision tree for undoing common Git mistakes without losing work.


1. The Emergency Decision Matrix

What happened?How to fix itCommand
I made local changes to a file and want to throw them awayDiscard unstaged changes in working treegit restore <file>
I accidentally ran git add on a fileUnstage file without losing changesgit restore --staged <file>
I made a commit locally and want to undo itUndo commit, keep changes stagedgit reset --soft HEAD~1
I committed with a typo in the messageAmend the commit messagegit commit --amend -m "new msg"
I committed to main instead of a feature branchCreate new branch, rewind maingit branch feat/my-work && git reset --hard origin/main
I accidentally deleted a fileRestore file from last commitgit restore <deleted-file>
I ran git reset --hard and lost commitsFind lost commit in refloggit reflog then git reset --hard <SHA>
I already pushed a buggy commit to origin/mainSafely revert public commitgit revert <SHA> then git push

2. Moving Commits to the Correct Branch

If you committed directly to main when you should have created a feature branch:

# 1. Create a new branch pointing at your current commit:
git branch feat/awesome-feature

# 2. Reset local main back to the remote origin/main:
git reset --hard origin/main

# 3. Switch to your newly created branch:
git switch feat/awesome-feature

On this page