Recovery
Choose the least destructive Git command for uncommitted, committed, or apparently lost work.
Before undoing anything, run git status and identify whether the change is untracked, unstaged, staged, committed, or published. Those states require different commands.
Save unfinished work
A temporary commit is usually the clearest checkpoint. Use a stash when you need a short-lived clean working tree and do not want the work in branch history.
-u includes untracked files but not ignored files. Restore with git stash apply so the stash remains available until you verify the files, then remove it with git stash drop.
Unstage without discarding the file
This removes the path from the next commit while leaving the working copy unchanged.
Discard an unstaged change
git restore replaces the working copy with the indexed version. The discarded edit is not recorded in normal Git history, so review the diff first.
Reverse a published commit
Use git revert for a commit that has reached a shared branch. It creates a new commit that applies the inverse change and preserves the existing history.
If the revert conflicts, resolve the files, stage them, and run git revert --continue. Use git revert --abort to return to the state before the revert attempt.
Repair a local commit
If the last commit is still private, you can add a missing file or improve its message with --amend.
Amending changes the commit ID. Do not amend a commit other people may have based work on.
Find a commit with the reflog
The reflog records recent changes to local references, including branch switches, resets, and rebases. It is local to your clone and eventually expires.
Create a recovery branch at the commit before changing anything else. Once the files are verified, cherry-pick or merge the recovered work into the correct branch.
Recovery rule Stop, inspect, and create a branch or tag before trying another history-changing command. Repeated resets often make the original problem harder to understand.