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

Git has no single “undo” command because an edit can be in your working files, staged in the index, recorded in a local commit, or already shared by pushing. First identify which state you want to change and whether the work must be kept. Then choose the matching operation and check git status; after changing history, inspect the log too.

First identify where the change is

Git work moves through separate steps: saving writes a file, staging selects content for the next commit, committing records the staged snapshot in local history, and pushing sends local commits to a remote. Undoing one step does not automatically undo the others. VS Code’s source control overview describes the same repository states available from the command line: an editor button is not a separate safety net.

  • Unstaged: changed in a tracked working file, but not selected for the next commit.
  • Staged: selected in the index for the next commit.
  • Committed: recorded in local history; it may or may not have been pushed.
  • Untracked: a new file Git is not tracking. The tracked-file restore command does not remove it.

Start with git status. If you are unsure what a tracked edit contains, inspect it with git diff for unstaged changes or git diff --staged for staged changes before acting.

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

How do I undo git add?

To remove a path from the index while keeping its edits in the working file, run:

git restore --staged <path>

This changes what is staged, not the edited file content. Confirm the result with git status; the path should appear as modified but unstaged. The older documented alternative is git reset HEAD <path>. See Pro Git’s “Undoing Things” chapter and VS Code’s staging and committing guide.

How do I discard an unstaged edit?

For a tracked path whose unstaged changes you intend to throw away, use:

git restore <path>

By default, Git restores the path from the index, replacing its current working-file content. That is destructive: inspect the diff first, or save a copy if there is any chance you will need the edits. Do not assume uncommitted work is recoverable afterward. This command addresses tracked paths; it is not a way to remove untracked files. Git’s restore documentation explains path restoration, including matching a path to its restore source.

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.

How do I undo my last local commit?

If the commit exists only on your machine and you want to revise local history, choose an operation based on what should happen to its changes. git reset moves the current branch to another commit; its mode determines what happens to the index and working tree. For example, to move back one commit:

git reset --soft HEAD~1

The commit is removed from the branch tip, while its changes remain staged. The reset modes differ as follows:

Mode Branch position Index Working tree
--soft Moved Unchanged Unchanged
--mixed (default) Moved Updated to the target commit Unchanged
--hard Moved Updated to the target commit Updated to the target commit

--hard can overwrite tracked, uncommitted working-tree changes. Do not use it as a casual cleanup command. An amend is another option when the goal is to correct the latest local commit; VS Code documents an “Undo Last Commit” action that keeps the changes staged. In either case, check git status and inspect git log to verify the new state. For the mechanics, see Pro Git’s “Reset Demystified” and VS Code’s staging and committing guide.

How do I undo a pushed commit?

When other people may already have based work on a commit, the usual shared-history choice is git revert. It adds a new commit that applies the inverse of the selected commit’s change; it does not erase the original commit from the shared sequence.

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

Use the commit identifier you intend to reverse, then review the result and follow your team’s normal process for pushing it. The ordinary revert operation expects a clean working tree. If conflicts occur, Git provides continuation or abort controls; resolve conflicts carefully before continuing. Reverting a merge commit requires selecting a mainline parent, and affects how later merges behave. Consult the official git revert documentation and your team’s practice before reverting a merge.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What if a committed tip seems to have disappeared?

If a reset or other branch-pointer move made a commit hard to find, git reflog can show recent positions of the local reference. Locate the wanted commit, inspect it, and create a branch at that commit to make it reachable again:

git reflog
# After identifying the intended commit:
git branch recovered-work <commit>

Reflog is a record of reference movement, not a backup of edits that were never committed. It does not guarantee recovery of overwritten working-file changes, and unreachable objects may eventually be pruned. See Pro Git’s maintenance and data recovery chapter.

Verify the outcome before continuing

  • Run git status and make sure each path is in the intended state: staged, unstaged, clean, or untracked.
  • For a history change, inspect git log to confirm the branch points where you expect.
  • If anything looks wrong, stop before staging or pushing more work. A later command can make recovery harder.

Git’s restore workflow is documented in Pro Git for Git 2.23.0 and later. Check your installed Git version if the command is unavailable; the older git reset HEAD <path> form can unstage a path.

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

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.