Git
Undo Checkout
Unstaged Files
Local Changes
Version Control

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.

Browse interview questions

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:

bash
git checkout -- path/to/file

or the newer equivalent:

bash
git restore path/to/file

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:

bash
git stash list
git stash show -p stash@{0}

2. Reflog

If a commit, reset, or branch movement happened around the same time, reflog may help with nearby history:

bash
git reflog

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:

bash
git restore app.py

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

bash
git stash push -m "wip before restore"

Or Make a Temporary Commit

bash
git add .
git commit -m "WIP backup"

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

bash
git switch -c scratch/experiment

That makes destructive cleanup commands less dangerous.

Modern Command Naming Helps

Git split the old checkout behavior into clearer commands:

  • 'git switch for branch switching'
  • 'git restore for 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 checkout or restore
  • 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 restore and old file-checkout commands as destructive operations

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions