Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

git - 撤消git rebase(Undoing a git rebase)

Does anybody know how to easily undo a git rebase?

(有人知道如何轻松撤消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

    (git签出两个分支的提交父对象)

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

(不只是一个)

  ask by webmat translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(最简单的方法是找到分支的头提交,就像在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).

(然后将当前分支重置为该分支(通常的警告是在使用--hard选项进行重置之前必须绝对确定)。)

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

(假设参考日志中的旧提交为HEAD@{5} :)

git reset --hard HEAD@{5}

In Windows, you may need to quote the reference:

(在Windows中,您可能需要引用以下引用:)

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

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

(您可以通过执行git log HEAD@{5}Windows: git log "HEAD@{5}" )来检查候选旧头的历史git log "HEAD@{5}" 。)

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.

(如果尚未对每个分支引用禁用禁用,则应该可以简单地执行git reflog branchname@{1}因为在重新附加到最终头之前,rebase会分离分支头。)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...