Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverting an interactive git rebase

Tags:

git

git-rebase

After completing a feature branch, during a git rebase -i I accidentally removed all my commits. I'm not completely sure but I suspect that instead of squashing my commits, I replaced the entire entry with a commit message.

http://shafiulazam.com/gitbook/4_interactive_rebasing.html says:

The last useful thing that interactive rebase can do is drop commits for you. If instead of choosing 'pick', 'squash' or 'edit' for the commit line, you simply remove the line, it will remove the commit from the history.

My question is: is there a way to revert/undo this?

like image 420
Zubin Avatar asked Mar 21 '11 07:03

Zubin


People also ask

How do I undo a git squash?

How to Undo a Merge Commit in Git. You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog .

How do I undo a rebase head in git?

To undo the rebase , we can use the reflog command of Git. Using git reflog , we can determine the branch's head commit immediately before the rebase starts.

How do I edit a rebase interactive?

Just right-click on the commit you want to edit and select Interactively Rebase from Here… Select reword on the commit and click Start Rebasing. Edit the commit message and click Resume Rebasing.


1 Answers

If you have just done the rebase, you can try as mentioned here:

git reset --hard ORIG_HEAD 

as Jakub Narębski details:

ORIG_HEAD is previous state of HEAD, set by commands that have possibly dangerous behavior, to be easy to revert them.
It is less useful now that Git has reflog: HEAD@{1} is roughly equivalent to ORIG_HEAD (HEAD@{1} is always last value of HEAD, ORIG_HEAD is last value of HEAD before dangerous operation).

If you have executed some operations since the rebase, the reflog can still help.

like image 121
VonC Avatar answered Sep 29 '22 20:09

VonC