Git undo changes in some files
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Undoing changes in selected files is a common Git task when only part of your working tree should be discarded. The correct command depends on file state: unstaged, staged, or already committed. Using targeted restore commands prevents accidental loss and avoids heavy operations that affect unrelated work.
Check State Before Undoing Anything
Start by inspecting what changed and where it is staged.
This step prevents using the wrong undo command for the wrong state.
Undo Unstaged Changes in Specific Files
If a file is modified in working tree but not staged, restore it from current HEAD.
Only listed files are reverted. Other local edits remain untouched.
Unstage a File but Keep Its Content
If a file is staged and you want to keep its edits locally:
This removes the file from index while preserving working tree modifications.
Discard Both Staged and Unstaged Edits for One File
To fully reset one file to current commit content:
This is destructive for that file. Verify with git diff first.
Restore File from an Older Commit
Sometimes you want a known past version, not current HEAD.
This creates an explicit forward commit and keeps history safe for collaborators.
Hunk-Level Undo for Partial Cleanup
When only part of a file should be undone, use interactive patch mode.
Git shows each hunk and lets you choose what to discard. This is useful when one file contains both keep and discard edits.
Already Committed and Pushed Changes
If bad changes are already shared, prefer a new corrective commit over history rewrite on shared branches.
This keeps team history linear and avoids force push risk.
Recovery If You Undo Too Much
If you accidentally discard useful work, inspect reflog quickly.
Reflog can often recover states even after destructive file restore commands.
Practical Safe Workflow
A repeatable sequence reduces mistakes:
- inspect status and both diffs
- choose smallest scope command
- verify status again
- run tests if behavior changed
- commit intentional final state
Small targeted undo steps are safer than one broad reset command.
Team Practices for Clear History
In collaborative repos, separate cleanup commits from feature commits when possible. One commit can restore files to baseline, and another can contain functional changes. This makes reviews easier and future reverts lower risk.
Also document unusual restores in commit messages, especially when restoring from non-head commits.
Safer Collaboration Defaults
On shared branches, prefer additive fix commits instead of force rewrites whenever possible. This keeps teammate history stable and reduces accidental rollback of unrelated work.
Common Pitfalls
A common pitfall is using repository-wide reset commands when only one file needed rollback.
Another issue is confusion between staged and unstaged states, which leads to unexpected kept or discarded changes.
Teams also rewrite published history unnecessarily when a forward fix commit would be safer and simpler.
Summary
- Choose undo command based on file state: unstaged, staged, or committed.
- Use
git restorefor precise file-level recovery. - Use patch mode for hunk-level selective undo.
- Prefer forward corrective commits on shared branches.
- Check
git statusand diffs before and after each undo operation.
Related reading
- Git undo local branch delete
- git update-index --assume-unchanged and git reset
- Git update submodules recursively
- Git vs Mercurial vs SVN
- Git What's the best practice to git clone into an existing folder?
- git, whitespace errors, squelching and autocrlf, the definitive answers
- Git with large files
- Git workflow and rebase vs merge questions
.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.