Git - How to undo a checkout of unstaged files which discards local changes
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you ran a checkout-style command that restored files and your unstaged local changes disappeared, the hard truth is that Git often cannot bring them back. Unstaged edits that were never committed, stashed, or otherwise recorded usually exist only in the working tree, so once they are overwritten, recovery depends more on editor history or filesystem snapshots than on Git itself.
Why Git Usually Cannot Undo This
Git is excellent at restoring data that was written into its object database. That includes:
- commits
- stashes
- staged blob objects that were recorded during certain operations
- references reachable through reflog or other history
Unstaged changes in your working directory are different. They are not a commit and usually are not stored in Git anywhere. So if you run a command that discards them, Git often has nothing to recover.
Classic examples include older commands such as:
or the newer equivalent:
Both replace the working-tree file content with another version. If the discarded content was never saved elsewhere, Git itself usually cannot reconstruct it.
What You Should Check First
Before assuming the work is gone forever, check whether it was recorded indirectly.
1. Stash List
If you stashed recently, the content may be there:
2. Reflog
If a commit, reset, or branch movement happened around the same time, reflog may help with nearby history:
Reflog is useful for recovering commits and references, but it usually does not resurrect purely unstaged edits that were never recorded.
3. IDE or Editor Local History
Many tools keep their own history:
- IntelliJ local history
- VS Code timeline or file history extensions
- PyCharm local history
- temporary swap or backup files
This is often the best recovery path for discarded unstaged work.
4. Filesystem Snapshots
If your machine uses Time Machine, Windows File History, filesystem snapshots, or backup software, check there next.
A Practical Example
Suppose you modified app.py, never staged it, and then ran:
At that point, app.py is replaced with the version Git restored. If you now run git status, the file may look perfectly clean, but that does not mean your edits are recoverable from Git. It only means the working tree now matches the restored content.
That is why commands that discard local changes should always be treated as potentially destructive.
Prevention Is the Real Fix
Because recovery is often impossible, the best strategy is prevention.
Stash Before Risky Commands
Or Make a Temporary Commit
You can later clean up the history with reset, rebase, or squash if needed. A temporary commit is vastly better than lost work.
Or Work on a Throwaway Branch
That makes destructive cleanup commands less dangerous.
Modern Command Naming Helps
Git split the old checkout behavior into clearer commands:
- '
git switchfor branch switching' - '
git restorefor restoring files'
That helps reduce accidental misuse, but it does not change the underlying risk. If you restore over unstaged work, the edits can still be lost.
If You Suspect the File Still Exists Somewhere
Sometimes the content is not truly gone, just not where you expect. Check:
- unsaved editor buffers
- temporary files in the project directory
- IDE recovery dialogs after restart
- operating system trash or backup caches
This is no longer Git recovery, but it can still save you.
Common Pitfalls
The biggest pitfall is assuming git reflog is a universal undo history for everything. It is not. Reflog tracks reference movement, not arbitrary working-tree edits.
Another pitfall is believing that Git silently saved your unstaged file content somewhere just because it knew the file existed. In normal workflows, it did not.
A third pitfall is using destructive restore commands casually because the file was "not committed yet anyway." That is exactly when the work is most fragile.
Finally, do not wait to learn a recovery habit after losing work once. Stash, local history, and lightweight WIP commits are cheap insurance.
Summary
- Git usually cannot recover unstaged changes that were overwritten by
checkoutorrestore - Reflog helps with references and commits, not normally with discarded working-tree edits
- Check stashes, IDE local history, and filesystem backups before giving up
- Prevention is the real answer: stash, make a temporary commit, or use a scratch branch
- Treat
git restoreand old file-checkout commands as destructive operations
Related reading
- Git - Ignore files during merge
- Git - Ignore node_modules folder everywhere
- git - pulling from specific branch
- Git - push current branch shortcut
- Git - Pushing code to two remotes
- git - remote add origin vs remote set-url origin
- git - remote add origin vs remote set-url origin
- Git - remote Repository not found
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.