linquize
linquize

Reputation: 20396

How to reverse a git rebase?

I know that the simplest way is to call git reflog to find out the original head of the branch, and than call git reset to the old commit.

But some situation reflog is not available, such as on a repository in another PC.

Any ways to reverse a git rebase and set to the old commit?

Upvotes: 0

Views: 1605

Answers (4)

Philip Oakley
Philip Oakley

Reputation: 14101

Simply use (the ref) ORIG_HEAD, which is set to point at the tip of the branch before the internal reset stage of the rebase. - see the manual

So checkout ORIG_HEAD, create a new branch name for it (stop it being a detached head) and you should be on your way (check it's all connected with gitk or your favoutite pretty one-line graph commands)

Upvotes: 0

Karl Bielefeldt
Karl Bielefeldt

Reputation: 49148

In that situation, there's no way to automatically "undo" the rebase, but you can manually do another rebase. For example, if you had:

A---B---C   <-- master
 \
  D---E---F <-- feature

And rebased to get:

A---B---C---D'---E'---F' <-- master

To get the branches back to the old state, do:

git checkout -b feature
git rebase --onto A C feature
git checkout master
git reset --hard C

Upvotes: 1

kan
kan

Reputation: 28991

When you do rebase you are rewriting the history. Branches will point to a different commits. Old commits are dropped off. You could list them by git fsck --lost-found and try to recover. However, they are available for garbage collection, so if you have done it a while ago, they could be irrecoverably lost.

Upvotes: 0

rks
rks

Reputation: 920

I'm not sure I understand your question. If I understand correctly you did some modifications on a repo, edited the history, pushed, pulled on another repository and want to undo the history edit from there? That can't be done, since you don't have the "original" history there.

As you said, git reflog is the way to go, and I don't why you wouldn't be able to use it (apart from the scenario I just sketched). Can you edit your post to give more information?

Upvotes: 3

Related Questions