Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Before undoing anything in Git, stop and inspect what is changed and where it lives. Run git status --short, git diff, and git diff --cached before using a command that could discard edits or move a branch. The right recovery depends on whether the work is uncommitted, staged, committed locally, or already shared.

First, find where the change lives

Git tracks file content in the working tree, the staging area (also called the index), and commits on a branch. A command that changes one of these may leave the others untouched. Start by checking the repository state:

git status --short
git diff
git diff --cached
  • git status --short shows changed, staged, and untracked paths.
  • git diff shows unstaged changes to tracked files.
  • git diff --cached shows staged changes that would go into the next commit.

These checks do not alter your files. If you are unsure whether work matters, do not run reset --hard, broad restore commands, or cleanup commands while investigating.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Undo edits that are not staged

To discard edits to one tracked file and replace its working-tree version with the staged version, use:

git restore -- path/to/file

If the file has no staged changes, this restores the version from the index, which will usually be the version in the current commit. Check the path and inspect git diff -- path/to/file first: the discarded edits are not preserved by this command.

To discard unstaged edits throughout the repository, use git restore . from the repository root only after reviewing the affected paths. A broad restore can erase useful work in every tracked file it touches. This does not remove untracked files.

Unstage changes without discarding the edits

If a file is staged but you want to keep editing it without including it in the next commit, remove it from the index:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
git restore --staged -- path/to/file

The file remains in your working tree; only its staged state is undone. To unstage all staged paths, use git restore --staged :/ from anywhere inside the repository.

Restore one file from an earlier commit

To inspect a file as it appeared in the parent of the current commit without changing your working tree, use:

git show HEAD^:path/to/file

To put that earlier version into the working tree, use:

git restore --source=HEAD^ -- path/to/file

This restores the selected version to the working tree; it does not move the branch or automatically stage the result. Review the change with git diff -- path/to/file, then stage it with git add -- path/to/file if you intend to commit it. Replace HEAD^ with the commit identifier or other valid revision you want to use.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Undo a local commit that has not been shared

When a commit exists only on your local branch, git reset can move the branch pointer backward. Its mode determines what happens to the index and working tree:

Command Branch Index Working tree Result
git reset --soft <commit> Moves to the chosen commit Kept as it was Kept as it was Leaves the undone commit’s changes staged.
git reset --mixed <commit> Moves to the chosen commit Reset to that commit Kept as it was Leaves the changes unstaged. This is the default mode if no mode is specified.
git reset --hard <commit> Moves to the chosen commit Reset to that commit Reset to that commit Can discard tracked staged and working-tree changes.

For example, to undo the latest local commit but keep its changes staged, use git reset --soft HEAD^. Choose the target revision deliberately: resetting to a commit changes the current branch’s history. Avoid --hard as a first response, especially if you have not inspected or saved the working-tree and index changes.

Undo a commit that is already shared

If other people may have fetched or based work on a commit, prefer a new commit that reverses it rather than moving the shared branch backward:

git revert <commit>

Revert applies the inverse of the selected commit as a new commit, preserving the shared history. Review the proposed change before completing it. If Git reports conflicts, resolve them as you would in a merge, stage the resolved files, and continue the revert with git revert --continue; to abandon the in-progress revert, use git revert --abort.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recover a commit after a reset, rebase, or branch move

If a commit disappeared from the current branch after a reset, rebase, or other reference movement, inspect the local reflog. It records recent positions of references in this repository:

git reflog
  1. Find the entry showing the commit or branch position from before the mistake.
  2. Inspect the candidate with git show <commit> or git log --oneline <commit>.
  3. If it is the work you need, create a recovery branch at that commit before changing the original branch: git branch recovery <commit>.
  4. Inspect the recovery branch and decide whether to switch to it, cherry-pick a commit, or move the original branch deliberately.

A reflog is local to your repository; it is not a record shared with collaborators, and its entries are not a permanent backup. Create the recovery reference before continuing experiments so the candidate commit remains reachable.

Find a deleted branch or a commit missing from the reflog

If the branch was deleted or the reflog does not show the commit, Git may still have unreachable objects in its object database. Search for unreachable commits with:

git fsck --full --no-reflogs --unreachable

For a candidate commit, inspect it with git show <commit>. If it contains the missing work, preserve it with a reference such as git branch recovery <commit>. This may not find every lost change: objects can be pruned, so recovery is not guaranteed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Set work aside or move a commit to another branch

Temporarily save uncommitted work

When you need to switch tasks or branches, stash tracked working-tree and index changes:

git stash push -m "work in progress"

This saves the changes and returns the working tree and index to the current branch tip. Later, apply the newest stash and remove it from the stash list with:

git stash pop

Check the resulting status and resolve any conflicts if they occur. Stashing is for uncommitted work; it does not recover a lost commit.

Apply a selected commit on the current branch

To bring a particular commit’s changes onto another branch, switch to the destination branch and cherry-pick the commit:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
git cherry-pick <commit>

If conflicts occur, resolve them and stage the resolved files, then run git cherry-pick --continue. To abandon the cherry-pick sequence and return to the state before it began, run git cherry-pick --abort. Cherry-pick applies a commit’s changes as a new commit on the current branch; it does not move the source branch.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.