Undoing a git rebase

前端 未结 18 1680
孤城傲影
孤城傲影 2020-11-22 02:41

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 t
18条回答
  •  猫巷女王i
    2020-11-22 03:19

    Resetting the branch to the dangling commit object of its old tip is of course the best solution, because it restores the previous state without expending any effort. But if you happen to have lost those commits (f.ex. because you garbage-collected your repository in the meantime, or this is a fresh clone), you can always rebase the branch again. The key to this is the --onto switch.

    Let’s say you had a topic branch imaginatively called topic, that you branched off master when the tip of master was the 0deadbeef commit. At some point while on the topic branch, you did git rebase master. Now you want to undo this. Here’s how:

    git rebase --onto 0deadbeef master topic
    

    This will take all commits on topic that aren’t on master and replay them on top of 0deadbeef.

    With --onto, you can rearrange your history into pretty much any shape whatsoever.

    Have fun. :-)

提交回复
热议问题