Git 如何回滚变基
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41049711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git how to rollback a rebase
提问by user1615666
In Git, how do you rollback a rebase if you're not happy with it?
在 Git 中,如果你对它不满意,你如何回滚它?
Git doesn't have dry-run for rebase. If I did a rebase and have not pushed it yet, how do I rollback to before, as if it never happened?
Git 没有用于 rebase 的试运行。如果我做了一个 rebase 并且还没有推送它,我如何回滚到以前,就好像它从未发生过一样?
回答by Robbie
You can use the reflogto find the first action before the rebase started and then reset --hard back to it. e.g.
您可以使用reflog在 rebase 开始之前找到第一个操作,然后将 --hard 重置回它。例如
$ git reflog
b710729 HEAD@{0}: rebase: some commit
5ad7c1c HEAD@{1}: rebase: another commit
deafcbf HEAD@{2}: checkout: moving from master to my-branch
...
$ git reset HEAD@{2} --hard
Now you should be back to before the rebase started.
现在你应该回到 rebase 开始之前。
To find the right place to reset to, you just pick the entry closest to top that doesn'tstart with "rebase".
要找到正确的重置位置,您只需选择最接近顶部且不以“rebase”开头的条目。
Alternative approach
替代方法
If the rebase is the only thingyou have done on the branch, i.e. you have no unpushed commits/changes - then you could just delete the local branch with git branch -D
and then check it out again:
如果 rebase 是您在分支上所做的唯一事情,即您没有未推送的提交/更改 - 那么您可以删除本地分支,git branch -D
然后再次检查它:
$ git checkout my-branch
$ git rebase master
// not happy with result
$ git checkout master
$ git branch -D my-branch
$ git checkout my-branch
Or for the same effect, you could reset --hard to the origin branch:
或者为了同样的效果,你可以将 --hard 重置为 origin 分支:
$ git reset --hard origin/my-branch
If you did do this while you had other unpushed commits, then you will have lost them. In that case just use the reflog approach above to jump back to the reflog entry where you made the commit(s).
如果您在有其他未推送的提交时这样做了,那么您将丢失它们。在这种情况下,只需使用上面的 reflog 方法跳回您进行提交的 reflog 条目。
回答by Meligy
Rebase keeps a backup of the old state as ORIG_HEAD
.
So you can revert the last rebase by running:
Rebase 将旧状态的备份保留为ORIG_HEAD
.
因此,您可以通过运行来恢复最后一次变基:
git reset --hard ORIG_HEAD