How to undo local changes to a specific file
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Undoing local changes to one file in Git depends on where those changes live. A file may be modified only in the working tree, staged in the index, or already committed locally. Each case uses a different command, so the first step is understanding the file’s current state.
Check the File State First
Start with:
This tells you whether the file is:
- modified but unstaged
- staged for commit
- both staged and further modified
Once you know that, choose the right restore command.
Undo Unstaged Changes
If the file is modified in the working tree but not staged, restore it from HEAD:
That discards your local edits and makes the file match the current commit.
Older examples often use:
That still appears in older material, but git restore is the clearer modern command for this task.
Unstage a File Without Losing the Edits
If the file is staged and you only want to remove it from the index, use:
This keeps your working-tree edits but removes the file from the next commit.
That is useful when you ran git add too broadly and want to keep editing before staging again.
Discard Both Staged and Unstaged Changes
If the file is staged and you also want to throw away the actual content changes, do it in two steps:
After those commands, the file matches HEAD again and no local edits remain.
Restore a File From a Different Commit
Sometimes “undo” means “replace the current file with the version from another commit.” In that case, specify a source revision:
That writes the file from the previous commit into your working tree. You can inspect it, stage it, and commit it if that is the version you want to keep.
This is safer than rewriting branch history when all you really need is one earlier version of one file.
When a Local Commit Already Exists
If you already committed locally and have not pushed yet, there are two general choices:
- create a new commit that restores the file
- rewrite local history
For a single-file correction, the first option is usually simpler and less risky:
That keeps history explicit and avoids rewriting other local work.
Use Diff Before You Discard Work
Before running restore commands, inspect what you are about to lose:
If the file is staged:
That small habit prevents a lot of accidental data loss.
Common Pitfalls
The biggest mistake is running git restore path/to/file without realizing it permanently discards unstaged edits in that file.
Another issue is confusing unstaging with restoring. git restore --staged changes the index, not the file contents in your working tree.
Developers also reach for git reset --hard when they only need to fix one file. That is much broader and can destroy unrelated work.
Finally, if the file includes user changes you might want later, stash or copy it before discarding anything.
Summary
- Use
git restore path/to/fileto discard unstaged changes in one file. - Use
git restore --staged path/to/fileto unstage it while keeping the edits. - Use both commands if you want to remove the file from staging and discard the edits.
- Use
git restore --source=<commit>when you want the file from another revision. - Check
git difffirst so you know exactly what will be lost.
Related reading
- How to unstage large number of files without deleting the content
- How to unstash only certain files?
- How to update a file in remote repo, without cloning that repo first?
- How to update a git shallow clone?
- How to update a pull request from forked repo?
- How to update git commit author, but keep original date when amending?
- How to update local tags to match remote?
- How to upgrade Git on Windows to the latest version
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.