Git Undo and Recovery Guide: Safely Revert Commits, Changes, and Lost Work
gitversion-controldebuggingdeveloper-workflowgithubcommand-line

Git Undo and Recovery Guide: Safely Revert Commits, Changes, and Lost Work

TThe Code Website Editorial Team
2026-11-16
7 min read

Choose the safest Git command to undo local changes, recover lost commits, reverse pushed work, and repair merge mistakes.

Git gives you several ways to undo a change, but they do not all have the same effect. This guide helps you choose between git restore, git reset, git revert, git stash, git reflog, and git cherry-pick without discarding work you still need.

Overview

The safest Git recovery command depends on two questions: where the change exists, and whether other people can already see it. A modified file in your working tree needs a different solution from a commit that has been pushed to a shared branch.

Use this decision table as a quick starting point:

SituationUsually appropriateMain caution
Discard edits in an unstaged filegit restore -- path/to/fileThe uncommitted edits are removed from that file.
Unstage a file but keep its editsgit restore --staged path/to/fileThe file remains modified in the working tree.
Temporarily set changes asidegit stash push -uRemember to inspect and apply the stash later.
Rewrite local commitsgit resetHard and mixed resets can make reachable work harder to find.
Undo a commit already sharedgit revertIt creates a new commit instead of changing existing history.
Recover a commit or branch referencegit reflog, then git branchReflog entries are local and should be used promptly.
Bring back one commit from another branchgit cherry-pickConflicts and duplicate changes are possible.

Before a destructive operation, run git status and, when practical, create a safety reference with git branch backup-before-undo. A temporary branch costs little and gives you another way back while you investigate.

What to track

Uncommitted changes

Start with:

git status
git diff
git diff --staged

If you want to discard edits in a specific working-tree file, use:

git restore -- path/to/file

To discard all tracked working-tree edits, use git restore . from the repository root. Treat this as destructive: it replaces files with the versions represented by the index, so copy or stash anything that may be useful first.

If a file was staged by mistake, unstage it while preserving the edits:

git restore --staged path/to/file

For work you may need later, stash it rather than deleting it:

git stash push -u -m "temporary work before cleanup"
git stash list
git stash show --stat stash@{0}

The -u option includes untracked files. Inspect a stash before applying it, particularly if the branch has changed. You can use git stash apply to keep the stash entry or git stash pop to apply it and remove it when the operation succeeds.

Local commits that have not been shared

Use git log --oneline --decorate -n 10 to confirm which commit you want to change. Then choose the reset mode deliberately:

git reset --soft HEAD~1
git reset HEAD~1
git reset --hard HEAD~1

A soft reset moves the branch pointer while keeping the undone commit's changes staged. The default mixed reset moves the pointer and leaves the changes in the working tree. A hard reset moves the pointer and updates both the index and working tree, which can discard uncommitted changes. Prefer soft or mixed reset when you are still deciding what to keep.

For a small correction to the latest commit, git commit --amend can replace that commit. Do not amend a commit that teammates have based work on unless your team has explicitly agreed on the history rewrite.

Pushed commits

For a commit already on a shared branch, the usual safe approach is to create an inverse commit:

git log --oneline
git revert <commit-id>git push origin <branch-name>

For the latest commit, git revert HEAD is often enough. For an older commit, provide its identifier. Review the generated change, run relevant tests, and push the new commit. This preserves the existing history while recording the correction.

Resetting a pushed branch and force-pushing can disrupt collaborators, open pull requests, and automation. If history must be rewritten, coordinate first, verify the remote branch, and prefer git push --force-with-lease over an unconditional force push. Even then, consider whether git revert better matches the situation.

Merge mistakes and selective recovery

If a merge is still in progress, inspect the conflict state with git status. When you want to abandon the in-progress merge, use:

git merge --abort

After a merge has been committed, reverting it requires choosing the parent that represents the mainline:

git revert -m 1 <merge-commit-id>

The correct parent depends on your branch structure, so inspect the merge commit before running the command. If you only need one change from another branch, cherry-pick that commit instead:

git switch target-branch
git cherry-pick <commit-id>

Cherry-pick creates a new commit on the current branch. Resolve conflicts carefully, then continue with git cherry-pick --continue, or abandon the operation with git cherry-pick --abort.

Cadence and checkpoints

Recovery is easier when a repository has a repeatable checkpoint routine rather than relying on memory during an incident. Before a reset, revert, merge, or cherry-pick, record:

  • The current branch: git branch --show-current.
  • The working-tree state: git status.
  • The recent history: git log --oneline --decorate --graph -n 15.
  • Any uncommitted work that must survive: a stash, patch file, or temporary branch.
  • Whether the target commit has been pushed or is used by another person.

For active repositories, make this review part of a monthly or quarterly maintenance check. Remove stale branches only after confirming they contain no unique work, and periodically verify that the team knows its policy for reverting release commits and handling force-push requests. When a release is underway, take the checkpoint immediately before a rollback rather than relying on an older log view.

After the operation, check the result with git status, inspect the diff, and run the repository's normal tests or build commands. A command can succeed syntactically while producing an unintended application change.

How to interpret changes

Think of Git as tracking several related states: the working tree, the staging area, the current commit, and branch references. Most undo mistakes happen when a command changes one state but the user expects it to change another.

git restore primarily changes file content in the working tree or index. git reset moves a branch reference and may also change the index and working tree depending on its mode. git revert leaves history intact and records an inverse change. git stash saves unfinished work outside the normal branch history. git reflog shows where local references previously pointed, which makes it useful after an accidental reset or branch deletion.

To recover a seemingly lost commit, search the local reflog:

git reflog --date=local
git show <candidate-commit-id>
git branch recovered-work <candidate-commit-id>

The new branch protects the recovered commit while you decide whether to merge, cherry-pick, or inspect it further. Reflog is not a substitute for backups: it describes local reference movements, and its entries may not be available on another clone. If you deleted a branch, look for the branch's recent position in git reflog --all or the repository's reflog output, identify the correct commit, and recreate the branch with git branch branch-name <commit-id>.

When a command produces conflicts, stop and inspect rather than stacking more recovery commands on top. Read conflict markers, compare the intended base and target, and use the relevant --abort command if the operation is no longer appropriate.

When to revisit

Return to this guide whenever you are about to run a destructive Git command, especially during a release, a difficult merge, or cleanup of old branches. The most useful recurring checkpoint is a short pre-operation pause:

  1. Run git status and save work that should not be lost.
  2. Confirm the branch and commit with git branch --show-current and git log --oneline -n 5.
  3. Decide whether the change is local or already shared.
  4. Choose restore, reset, revert, stash, reflog, or cherry-pick based on that distinction.
  5. Inspect the resulting diff and run tests before pushing.

Revisit the team workflow monthly or quarterly, and update its examples when Git behavior, IDE integrations, or hosting workflows change. Verify commands in a disposable clone before documenting them for others. When in doubt, preserve the current state with a branch or stash first; investigation is safer when the original work remains recoverable.

For related debugging workflows, see the Browser DevTools Network Panel Guide for tracing frontend requests, the package.json and lockfile guide for dependency changes, and the HTTP status codes troubleshooting guide for diagnosing failures after a deployment.

Related Topics

#git#version-control#debugging#developer-workflow#github#command-line
T

The Code Website Editorial Team

Technical 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.