
You run git add, then realize one file belongs in a different commit. You do not want to delete your edits. You only want to remove that file's changes from the next commit. I would separate those two goals before using any command with "restore" in its name.
Git keeps a working copy on disk and an index, often called the staging area. Unstaging changes the index. Discarding edits changes the working copy. The difference is small on the command line and large when the file contains an hour of work.
Check that this is the right situation
This example is for a normal repository with an existing commit, a regular tracked file and no unresolved merge conflict. The example path is src/report.py. Replace it with your actual path from the repository root. Save open editor buffers first, and make a separate copy of work you cannot afford to lose.
Inspect the current state before changing anything:
git status --short
git diff --cached -- src/report.py
git diff -- src/report.py The cached diff shows changes staged for the next commit relative to HEAD, the current commit. The plain diff compares the working copy with the index. A file can have both staged edits and newer unstaged edits, so one diff alone does not tell the whole story.
Read the two status columns carefully
Outside an unresolved merge, Git's short status uses its first column for the index and its second for the working tree. For a regular modified file, these illustrative lines mean different things:
M src/report.py
M src/report.py
MM src/report.py The first line means a modification is staged and the working copy matches that staged version. The second means the modification is not staged. The third means the index contains a modification and the working copy differs from it again. These are alternatives, not three lines you should expect for the same path at once.
If status reports an unresolved conflict, an unexpected deletion, a rename or a submodule state, stop and inspect that situation separately. The short-format conflict codes have different meanings; do not apply the simple two-column interpretation blindly.
Restore only the staged copy
For the scoped situation above, use the explicit staged-only form:
git restore --staged -- src/report.py Git documents that --staged alone restores the index, using HEAD as the default source. It does not request a working-tree restore. Your current file on disk is left in place, while the staged version of that path is reset to the committed version.
This also means any carefully selected partial staging for that file is removed. If you had staged only two hunks but kept other edits unstaged, this whole-file command unstages that path's staged changes, not just one selected hunk. The local edits remain for you to review and stage again deliberately.
Verify the result before the next commit
git status --short
git diff --cached -- src/report.py
git diff -- src/report.py For a modified tracked file successfully unstaged against HEAD, the path-specific cached diff should now be empty. The plain diff should show the local changes that remain. Open the file and confirm the text you wanted to keep is still there. Other paths may still be staged; review the complete staged diff before committing anything.
Do not shorten the command into a different operation
Without --staged, Git restore defaults to the working tree. Adding --worktree alongside --staged targets both locations. Those forms are not substitutes for keeping local edits while unstaging. Do not copy a discard-changes recipe just because it uses the same command name.
This workflow does not undo an existing commit, remove secrets from history or resolve a merge. It also assumes HEAD exists. If this is a brand-new repository before its first commit, do not force a HEAD-based recipe through an error. Inspect that case separately. The commands here are a documentation-based example, not a claim that they were run on your project.
Comments
Post a Comment