Choose by the state of your work
| Situation | Command | Result |
|---|---|---|
| Unstage a file | git restore --staged -- file.txt | Keeps the edits in your working tree. |
| Undo the last local commit, keep edits staged | git reset --soft HEAD~1 | Moves the branch back one commit. |
| Reverse a commit already shared | git revert <commit> | Adds a new commit reversing that change. |
Check git status and git log --oneline -5 first. The reset example needs a parent commit and applies to a private branch. A merge commit needs separate consideration; do not guess a mainline parent.
Try a local reset in a disposable repository
Use a new, empty directory outside your project. This exercise creates two commits, removes the second from the branch history and leaves its edit staged.
git init -b main
git config user.name "DevSheet Demo"
git config user.email "demo@example.invalid"
printf "one\n" > note.txt
git add note.txt
git commit -m "First note"
printf "two\n" >> note.txt
git commit -am "Second line"
git reset --soft HEAD~1
git status --short
git diff --cachedExpected: M note.txt in the short status, and an added two line in the staged diff. The file still contains both lines.
Avoid losing work
git reset --hard can discard uncommitted tracked edits and overwrite obstructing untracked paths. It is unnecessary for the exercise above.
git restore -- file.txt replaces unstaged tracked edits; adding --staged changes the index instead. If a branch was moved by mistake, inspect git reflog and create a recovery branch at the verified old commit. Reflog cannot recover every uncommitted edit.