How to undo git checkout

Accidentally checking out files or branches can overwrite uncommitted changes, but Git provides mechanisms to recover. With over 25 years of software development experience and as the creator of CoreUI, I’ve recovered from countless accidental checkouts. Git’s reflog tracks all branch movements, and modern Git provides the restore command as a safer alternative to checkout. This approach helps you undo file checkouts, recover lost changes, and safely switch branches.

Use reflog, restore, or stash to undo git checkout and recover accidentally overwritten changes.

Undo file checkout (restore changes):

# You accidentally ran: git checkout -- file.js
# This overwrote your local changes

# Check reflog for the commit before checkout
git reflog

# Output shows:
# abc1234 HEAD@{0}: checkout: moving to main
# def5678 HEAD@{1}: commit: Your work in progress

# Restore file from previous commit
git show HEAD@{1}:file.js > file.js

# Or checkout file from specific commit
git checkout HEAD@{1} -- file.js

Undo branch checkout:

# You accidentally switched branches
git checkout wrong-branch

# Go back to previous branch
git checkout -

# Or use reflog to find previous HEAD
git reflog
git checkout HEAD@{1}

Modern approach with git restore:

# Modern Git (2.23+) uses restore instead of checkout

# Undo file restore (you ran: git restore file.js)
# If you have uncommitted changes, use stash first
git stash push -m "Backup before checkout"

# Restore from stash
git stash pop

# Or restore from specific stash
git stash list
git stash apply stash@{0}

Recover unstaged changes:

# You ran: git checkout -- .
# This discarded all unstaged changes

# Check if changes exist in a stash
git stash list

# If you had auto-stash enabled
git stash pop

# Check reflog for dangling commits
git fsck --lost-found
git show <hash>

Undo checkout of deleted file:

# You deleted a file and want to restore it
git checkout HEAD -- deleted-file.js

# Or with modern syntax
git restore deleted-file.js

# Restore file from specific commit
git restore --source=HEAD~3 file.js

Prevent accidental checkouts:

# Use git switch for branches (Git 2.23+)
git switch main
git switch -c new-branch

# Use git restore for files
git restore file.js
git restore --staged file.js

# These commands are safer and more explicit

Check what will be overwritten:

# Before checking out, see what would change
git diff HEAD -- file.js

# Check if you have uncommitted changes
git status

# Stash changes before switching branches
git stash push -m "Before switching to main"
git checkout main
git stash pop

Recover from detached HEAD:

# You accidentally checked out a commit
git checkout abc1234

# You're now in detached HEAD state
# Create branch to save work
git switch -c recovery-branch

# Or go back to previous branch
git switch -

Best Practice Note

Modern Git separates git switch (for branches) from git restore (for files), making commands clearer and safer. Always stash uncommitted changes before switching branches to avoid losing work. Use git checkout - to return to the previous branch quickly. The reflog keeps track of all HEAD movements for 90 days, allowing recovery of most checkout mistakes. Check git status before checking out to see what changes will be lost. This is how we handle Git in CoreUI development—using modern commands, checking status before destructive operations, and relying on reflog for recovery when needed.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author