Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undoing a git rebase

Does anybody know how to easily undo a git rebase?

The only way that comes to mind is to go at it manually:

  • git checkout the commit parent to both of the branches
  • then create a temp branch from there
  • cherry-pick all commits by hand
  • replace the branch in which I rebased by the manually-created branch

In my current situation this is gonna work because I can easily spot commits from both branches (one was my stuff, the other was my colleague's stuff).

However my approach strikes me as suboptimal and error-prone (let's say I had just rebased with 2 of my own branches).

Any ideas?

Clarification: I'm talking about a rebase during which a bunch of commits were replayed. Not only one.

like image 218
webmat Avatar asked Sep 25 '08 17:09

webmat


People also ask

How do I cancel rebase in progress?

If you prefer to skip this patch, run "git rebase --skip" instead. To check out the original branch and stop rebasing, run "git rebase --abort".

How do I undo a rebase in Intellij?

Open the repository folder in your terminal (linux, osx) or in the Git Bash (windows). Let's abort and start again, execute in the terminal: "git rebase --abort" . This command will revert your master to the HEAD state before you start the rebase.

Can you undo a rebase 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.


1 Answers

The easiest way would be to find the head commit of the branch as it was immediately before the rebase started in the reflog...

git reflog 

and to reset the current branch to it (with the usual caveats about being absolutely sure before reseting with the --hard option).

Suppose the old commit was HEAD@{2} in the ref log:

git reset --hard HEAD@{2} 

In Windows, you may need to quote the reference:

git reset --hard "HEAD@{2}" 

You can check the history of the candidate old head by just doing a git log HEAD@{2} (Windows: git log "HEAD@{2}").

If you've not disabled per branch reflogs you should be able to simply do git reflog branchname@{1} as a rebase detaches the branch head before reattaching to the final head. I would double check this, though as I haven't verified this recently.

Per default, all reflogs are activated for non-bare repositories:

[core]     logAllRefUpdates = true 
like image 117
CB Bailey Avatar answered Oct 17 '22 23:10

CB Bailey