Get the free coreGet core

Git undo reset: get commits back after git reset --hard

Updated 5 min readSection 04 / Incidents & recovery
SHORT ANSWER

For a git undo reset, first save where your branch is now, then look in the reflog, which records when the tips of branches were updated in your local repository. Find the entry from before the reset and move the branch back to it with git reset --hard, which overwrites files in your working tree. HEAD@{2} means where HEAD used to be two moves ago. Changes you never committed do not come back this way.

A hard reset feels final, because the commits vanish from git log. They are usually still there. Git keeps a record of where your branch pointed, and you can go back to it. This page gives the order to do it in and the limits of the method. Before any git undo reset attempt, read the warnings, because the commands that bring work back are the same ones that can overwrite it.

01Terms you need

HEAD is the commit you currently have checked out, the latest one on your branch. The working tree is the files in your project folder as you see them. The index, or staging area, is the list of changes you have added for the next commit. The reflog is Git's local log of where HEAD and your branches used to point. ORIG_HEAD is a marker Git sets to the branch tip before a reset.

02Git undo reset in four steps

  1. Save the current state first. Warning: moving a branch back with git reset --hard overwrites files and may overwrite untracked files. A branch keeps commits only: edits you have not committed are not saved by it, and git reset --hard overwrites all files with the version from the commit you name. If you have uncommitted edits you want, commit them first. Then mark where the branch is now with the first line of Git's documented safe order:
git branch topic/wip
  1. List the reflog. Run git reflog, which only reads and changes nothing. The reflog covers all recent actions, and the HEAD reflog also records branch switching.
git reflog
  1. Pick the line from before the reset. Each entry is one move of HEAD, and the reset you want to undo is one of those moves. Each entry has a label like HEAD@{2}, which means where HEAD used to be two moves ago. Read the commit messages next to the labels and find the line that still shows your lost work. Choose the one just before the reset, and note its label. If you pick the wrong line, nothing is lost: the branch you saved in step one still points to where you started, so you can reset to it and try another line.
  2. Move the branch back. Use the command in the next section with that label.

03Move the branch back

Warning: this overwrites all files and directories with the version from that commit, and may overwrite untracked files. Anything you changed since the reset and did not commit goes too. In the command below, replace 2 with the number from your own line:

git reset --hard HEAD@{2}

If you saved your branch in step one, you can switch to it later to get back to where you started:

git switch topic/wip

04The ORIG_HEAD shortcut

Before a reset, Git sets ORIG_HEAD to the tip of the current branch. So right after the reset you regret, ORIG_HEAD still holds the commit the branch pointed to before it. The same shortcut undoes a merge or pull: resetting hard to ORIG_HEAD brings your index and working tree back to that state and resets the tip of the branch to that commit. Warning: the command discards local changes you have not committed:

git reset --hard ORIG_HEAD

If you have run other Git commands since the reset and are not sure where ORIG_HEAD points, use the reflog entry instead, because the reflog shows the whole list.

05How long the reflog lasts

The reflog is not permanent. Git removes reflog entries older than 90 days by default. Entries that are not reachable from the current tip expire sooner, after 30 days by default. A commit you reset away is no longer reachable from the branch, so the shorter limit applies to it. While an entry exists, git gc, Git's cleanup command, keeps the objects that reflogs reference, including commits on branches that were later amended or rewound. These are defaults, and a repository set up by someone else may differ. Do not wait.

06What the undo cannot bring back

What you lostDoes the reflog help?
Commits that a reset moved the branch away fromYes, the reflog records where branch tips were
Edits you never committedNo, a hard reset overwrites them in the working tree
Files Git never trackedNo, they may be overwritten too

For files Git never tracked, read restore untracked files. For one deleted path, see restore a deleted file in Git. If you mean git restore rather than git reset, it writes contents from a restore source into your working tree, and edits that were never staged or committed have no copy in Git to go back to.

07Why reset can hurt, and what revert does

git reset --hard moves your branch and overwrites files. git revert records new commits that reverse earlier ones, so history is added to, not rewound. If you only want to cancel a commit others may already have, revert leaves the earlier commit in history and adds one on top.

08When an AI agent runs the commands

Auto mode in Claude Code blocks git reset --hard and similar commands, because its classifier, the check that screens each action, presumes they would discard uncommitted changes. The auto mode page has the full list. Rewinding inside the tool is covered by Claude Code undo and Claude Code checkpoints, and Cursor has its own in Cursor restore checkpoint.

09Git undo reset: what to remember

Save the branch first. Open the reflog, pick the entry from before the reset, and move back with git reset --hard. ORIG_HEAD is the shortcut right after the reset or after a merge or pull. The reflog expires after 90 days, or 30 for unreachable entries. Uncommitted changes do not come back this way.

FAQ

How do I undo a reset?

Save where the branch is now, find where it was in the reflog, then move it back with git reset --hard and that position. HEAD@{2} means where HEAD used to be two moves ago. Moving back overwrites files in your working tree.

What is the difference between git reset and git revert?

git reset --hard overwrites all files and directories with the version from the commit you name. git revert records new commits that reverse earlier ones, so history is added to, not rewound.

How long does the reflog keep entries?

Reflog entries expire after 90 days by default. Entries that are not reachable from the current tip expire after 30 days by default.

Does git reset --hard remove untracked files?

It may overwrite them: Git's manual says --hard overwrites all files and directories with the version from the commit, and may overwrite untracked files.

What is ORIG_HEAD?

Before a reset, ORIG_HEAD is set to the tip of the current branch. After a pull or merge, resetting hard to it brings your index file and working tree back to that state, and resets the branch tip to that commit.

Can Claude Code run git reset --hard on its own?

It depends on the mode. Auto mode blocks git reset --hard, because its classifier, the check that screens each action, presumes the command would discard uncommitted changes. In manual mode, Claude Code asks before Bash commands except a built-in set of read-only ones.

Sources

  1. Git documentation, git-reset
  2. Git documentation, git-reflog
  3. Git documentation, git-gc
  4. Git documentation, git-revert
  5. Git documentation, git-restore
  6. Claude Code docs, permission modes
  7. Artificial Immunity
Did an agent run git reset on your repo? Artificial Immunity watches the Mac where your agents work.Get the free core