Git Recovery Guide: Undo Commits, Restore Files, and Resolve Mistakes Safely
Gitversion controldeveloper productivitycommand linedebugging

Git Recovery Guide: Undo Commits, Restore Files, and Resolve Mistakes Safely

PPrograma Space Editorial Team
2026-12-01
6 min read

A practical Git recovery guide covering restore, revert, reset, stash, reflog, and safe ways to recover files, commits, and deleted branches.

Git mistakes are usually recoverable, but the safest command depends on whether the work is committed, shared, staged, or still only in your working directory. This guide compares restore, revert, reset, reflog, stash, and branch recovery, then gives repeatable workflows for undoing commits and restoring files without losing valuable work.

Overview

Before changing Git history, identify three facts: where the mistake exists, whether the commit has been pushed, and whether you need to keep the current file changes. These details determine whether you should create a new corrective commit or move a branch pointer backward.

A useful rule is simple: use git revert for history that has already been shared, and consider git reset for private local history that has not been pushed. Use git restore when the problem is an unwanted file change rather than a commit. If a branch or commit appears to be missing, git reflog can help locate earlier positions of your local references.

SituationUsually appropriateWhy
Discard unstaged changes in one filegit restore -- pathReplaces the working-tree file with the version from the index.
Unstage a file but keep its editsgit restore --staged -- pathRemoves the file from the index without deleting the working change.
Undo a pushed commitgit revert <commit>Creates a new commit that reverses the earlier one.
Remove private local commitsgit resetMoves the current branch reference; it changes local history.
Temporarily set aside uncommitted workgit stash pushStores changes so you can switch tasks or branches.
Recover a deleted or moved local referencegit reflogShows recent movements of references such as HEAD.

When uncertain, pause before using a destructive-looking command. Make a safety branch or tag first:

git status
git branch safety-copy

A safety reference does not replace a backup, but it gives you a named pointer to the current state while you investigate.

What to track

1. The location of the mistake

Run git status and inspect the recent history:

git status
git log --oneline --decorate -n 8

Determine whether the unwanted change is unstaged, staged, included in the latest commit, or several commits behind HEAD. Also check whether your branch is ahead of or behind its remote tracking branch. A mistake in the working tree calls for a different recovery path from a mistake already published to a shared branch.

2. Whether changes must be preserved

Never assume a reset or restore will preserve uncommitted work. If the edits matter, commit them on a temporary branch or stash them before experimenting. A temporary commit is often easier to inspect and recover than an unnamed stash:

git switch -c rescue-work
git add -A
git commit -m "WIP: preserve local recovery state"

If a temporary commit is not appropriate, use:

git stash push -u -m "before Git recovery"

The -u option includes untracked files. Review the stash afterward with git stash list, and restore it with git stash pop or git stash apply. The latter keeps the stash entry until you confirm that the changes are safely restored.

3. Whether history is shared

Ask whether another person, build system, or deployment process may already depend on the commit. If the answer is yes or unclear, prefer a new commit over rewriting the branch. This distinction is the practical difference in the common git reset versus git revert decision.

Cadence and checkpoints

Undo local file changes

To discard unstaged changes to a specific file, use:

git restore -- path/to/file

To restore the file from a particular commit:

git restore --source=<commit> -- path/to/file

Review the result with git diff before committing. To unstage a file while retaining its edits, run:

git restore --staged -- path/to/file

These commands operate on files, not on the branch's commit history. That makes them a good first choice when the mistake is limited to a working-tree or staging decision.

Undo the latest local commit

If the latest commit is private and you want to keep its changes staged, use:

git reset --soft HEAD~1

To keep the changes but unstage them, use:

git reset --mixed HEAD~1

--mixed is the default mode. To remove the commit and its changes from the working tree, use git reset --hard HEAD~1 only when you have verified that the content is disposable or safely backed up. A hard reset can make uncommitted work difficult to recover.

Undo a pushed commit safely

For a commit that is already shared, create an inverse commit:

git revert <commit>
git push

For the latest commit, git revert HEAD is often sufficient. If the original change conflicts with later work, Git will pause for conflict resolution. Inspect the affected files, stage the resolved versions, and continue with git revert --continue. If you need to stop the operation, use git revert --abort when available for the in-progress revert.

Recover a deleted branch or lost commit

A deleted local branch does not necessarily mean its commits are immediately unreachable. Inspect the reference log:

git reflog --all

Look for the commit position from before the deletion, reset, rebase, or checkout. Confirm the candidate commit:

git show <commit>

Then create a new branch at that commit:

git switch -c recovered-work <commit>

Reflog entries are local records and are not a substitute for a remote backup. They may also become unavailable as local maintenance removes old unreachable objects, so investigate promptly when a reference disappears.

How to interpret changes

Recovery is complete only when the repository state matches your intention, not merely when a command finishes without an error. After each operation, check:

  • Working tree: Run git status and confirm whether remaining changes are expected.
  • Diff: Use git diff and, when relevant, git diff --cached to inspect unstaged and staged content.
  • History: Run git log --oneline --decorate -n 8 to verify the branch position and commit message.
  • Validation: Run the project's tests, formatter, linter, or build before pushing.
  • Remote state: If the change was shared, verify that the corrective commit is visible on the expected branch.

Do not use git push --force as a routine fix for a shared-branch mistake. If history truly must be rewritten, coordinate with collaborators and use the least risky force-push option supported by your workflow, such as a force push that checks whether the remote changed since your last fetch. The exact policy belongs to the repository team; the general principle is to make history changes explicit and recoverable.

When a revert produces conflicts, resolve the conflict based on the desired final behavior rather than blindly accepting one side. Read the original change, the later changes, and the tests that cover the affected area. This is especially important when undoing database, API, configuration, or dependency changes, where a clean textual merge can still produce an invalid runtime state.

When to revisit

Keep this guide available as a practical recovery checklist, and revisit it whenever your team changes its branching or release workflow. A monthly or quarterly review is useful for checking that developers know where temporary recovery branches belong, how shared branches are protected, and which commands are acceptable in production repositories.

Update your local checklist when your Git version, hosting workflow, hooks, or CI rules change. Also revisit it after a significant incident: a deleted branch, accidental force push, failed rebase, or recovery involving generated files. Record what happened, which checkpoint would have caught it earlier, and whether a safer habit should become standard.

Before your next risky Git operation, use this short sequence:

  1. Run git status and save or commit valuable uncommitted work.
  2. Confirm whether the target commit is shared.
  3. Create a safety branch if the state is difficult to reproduce.
  4. Choose restore for files, revert for shared history, reset for private history, or reflog for local recovery.
  5. Inspect the diff, history, tests, and remote state before pushing.

For related debugging habits, see the GitHub Actions debugging checklist when a recovery affects CI, and use the npm dependency rollback checklist when the mistake involves package updates. The safest Git recovery process is repeatable: preserve first, change the smallest possible surface, verify the result, and communicate before rewriting shared history.

Related Topics

#Git#version control#developer productivity#command line#debugging
P

Programa Space Editorial Team

Developer Resources Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.