Cubis Engineers

Recovery

Choose the least destructive Git command for uncommitted, committed, or apparently lost work.

Developer workflowIntermediateUpdated Aug 13, 2026gitrecoveryrevertrestorereflog

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.

Terminal
git stash push -u -m "wip: health-check debugging"
git stash list
git stash show --stat stash@{0}

-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

Terminal
git restore --staged path/to/file

This removes the path from the next commit while leaving the working copy unchanged.

Discard an unstaged change

Terminal
git diff -- path/to/file
git restore path/to/file

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.

Terminal
git show <commit>
git revert <commit>
git show --stat HEAD

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.

Terminal
git add path/to/missing-file
git commit --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.

Terminal
git reflog --date=local
git show HEAD@{2}
git switch -c recovery/lost-work <commit>

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.

References

On this page