Skip to content

ORIG_HEAD recovery

ORIG_HEAD recovery is a safety mechanism in [[Git]] used to restore a repository's state immediately after a risky operation, such as a problematic rebase.^[600-developer-tools-git-git-rebase.md, 600-developer__tools__git__git-rebase.md]

Overview

When a "destructive" command like git rebase is executed, Git automatically saves the previous state of the branch reference to a reference called ORIG_HEAD before proceeding.^[600-developer-tools-git-git-rebase.md, 600-developer__tools__git__git-rebase.md] This allows users to effectively "undo" the operation if the result is unintended or if errors occur during the process.

Usage

To recover the state saved in ORIG_HEAD, users can utilize the git reset command.

  • Hard Reset: The specific command git reset ORIG_HEAD --hard is used to forcefully revert the branch to the state captured in ORIG_HEAD, discarding any subsequent changes or commits created by the failed operation.^[600-developer-tools-git-git-rebase.md, 600-developer__tools__git__git-rebase.md]

Use Cases

This method is commonly employed when a rebase operation results in conflicts or is executed incorrectly (for example, rebasing the current branch onto a descendant instead of an ancestor), requiring a complete reversal to the starting point.^[600-developer-tools-git-git-rebase.md, 600-developer__tools__git__git-rebase.md]

Sources

  • 600-developer-tools-git-git-rebase.md
  • 600-developer__tools__git__git-rebase.md