Recover from an unsuccessful git rebase with the git reflog command

The git rebase command allows you to adjust the history of your Git repository. It's a useful feature, but of course, mistakes can be made. Use the git reflog command to recover.
Register or Login to like
Woman sitting in front of her laptop

kris krüg

The git rebase command allows you to adjust the history of your Git repository. It's a useful feature, but of course, mistakes can be made. As is usually the case with Git, you can repair your error and restore your repository to a former state. To recover from an unsuccessful rebase, use the git reflog command.

Git reflog

Suppose you perform this interactive rebase:

$ git rebase -i HEAD~20

In this context, ~20 means to rebase the last 20 commits.

Unfortunately, in this imaginary scenario, you mistakenly squashed or dropped some commits you didn't want to lose. You've already completed the rebase, but this is Git, so of course, you can recover your lost commits.

Review your history with reflog

 Run the git reflog command to collect data and view a history of your interactions with your repository. This is an example for my demonstration repository, however, the result will vary depending on your actual repository:

$ git reflog
222967b (HEAD -> main) HEAD@{0}: rebase (finish): returning to refs/heads/main
222967b (HEAD -> main) HEAD@{1}: rebase (squash): My big rebase
c388f0c HEAD@{2}: rebase (squash): # This is a combination of 20 commits
56ee04d HEAD@{3}: rebase (start): checkout HEAD~20
0a0f875 HEAD@{4}: commit: An old good commit
[...]

Find the last good commit

In this example, HEAD@{3} represents the start of your rebase. You can tell because its description is rebase (start).

The commit just under it, 0a0f875 HEAD@{4}, is the tip of your Git branch before you executed your incorrect rebase. Depending on how old and active your repository is, there are likely more lines below this one, but assume this is the commit you want to restore.

Restore the commit

To recover the commit you accidentally squashed and all of its parent commits, including those accidentally squashed or dropped, use git checkout. In this example, HEAD@{4} is the commit you need to restore, so that's the one to check out:

$ git checkout HEAD@{4}

With your good commit restored, you can create a new branch using git checkout -b <branch_name> as usual. Replace <branch_name> with your desired branch name, such as test-branch.

Git version control

Git's purpose is to track versions, and its default setting is usually to preserve as much data about your work as feasible. Learning to use new Git commands makes many of its most powerful features available and safeguards your work.

Tags
Avatar
Agil has more than 6 years of experience as a technical writer that specialises in producing accurate, clear, and concise documentation for software products. Agil has the ability to communicate technical ideas to a variety of audiences, including developers, engineers, and end users.
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.