Course contentsShow
← Data Science
Lesson 2021 of 2,14546. Version Control with GitFree lesson

Recovering from Rebase Mistakes

Use reflog and other Git recovery techniques to undo problematic rebases and restore previous states.

Recovering from Rebase Mistakes

You'll learn how to use Git's reflog and recovery techniques to undo problematic rebases and restore your repository to a safe state.

Even experienced practitioners make rebase mistakes—applying changes to the wrong branch, losing commits during an interactive rebase, or creating an unworkable history. Fortunately, Git keeps a safety net.

Understanding the Reflog

Git's reflog (reference log) records every movement of HEAD and branch pointers in your local repository. Think of it as Git's "undo history"—a chronological diary of where your branches have been, even after commits appear "lost."

When you rebase and things go wrong, those commits aren't truly deleted—they're just no longer referenced by any branch. The reflog lets you find and restore them.

Recovery Workflow

To recover from a bad rebase:

  1. View the reflog: git reflog shows recent HEAD movements with timestamps and commit hashes
  2. Identify the pre-rebase state: Look for the entry just before you started the problematic rebase
  3. Reset to that state: git reset --hard <commit-hash> restores your branch to that exact point

The reflog typically preserves entries for 90 days by default, giving you a generous recovery window.

Additional Recovery Techniques

Beyond reflog, you can:

  • Use git reflog show <branch-name> to see a specific branch's history
  • Recover individual commits with git cherry-pick <commit-hash> after finding them in the reflog
  • Use git fsck --lost-found to locate truly orphaned commits if reflog entries have expired

If you've already pushed a bad rebase to a remote and others have pulled it, recovery becomes more complex—this underscores why The Golden Rule of Rebase exists.

Key Takeaway: Git's reflog is your time machine for recovering from rebase mistakes—use git reflog to find previous states and git reset --hard to restore them, making most rebase errors reversible.