撤消 git rebase [英] Undoing a git rebase

查看:23
本文介绍了撤消 git rebase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何轻松撤消 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 提交父级到两个分支
  • 然后从那里创建一个临时分支
  • 手动挑选所有提交
  • 用手动创建的分支替换我重新定位的分支

在我目前的情况下,这是行得通的,因为我可以轻松地发现来自两个分支的提交(一个是我的东西,另一个是我同事的东西).

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).

然而,我的方法让我觉得不是最理想的并且容易出错(假设我刚刚用我自己的 2 个分支重新定位).

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

有什么想法吗?

澄清:我说的是在重放期间重放一堆提交的变基.不止一个.

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

推荐答案

最简单的方法是在 reflog...

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

并将当前分支重置为它(通常的警告是在使用 --hard 选项重置之前绝对确定).

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

假设旧提交是引用日志中的 HEAD@{2}:

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

git reset --hard HEAD@{2}

在 Windows 中,您可能需要引用引用:

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

您只需执行一个git log HEAD@{2} (Windows: git log "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}").

如果您没有禁用每个分支的引用日志,您应该能够简单地执行 git reflog branchname@{1} 作为 rebase 在重新附加到最终头之前分离分支头.我会仔细检查一下,虽然我最近没有验证过.

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

这篇关于撤消 git rebase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆