简体   繁体   English

Git:如何变基到以前的提交

[英]Git: how to rebase onto previous commit

I'm working on branch C which is based on branch B, then branch A:我正在处理基于分支 B 的分支 C,然后是分支 A:

A---B---C

Is there any commands that can make branch C directly based on branch A like this:有没有什么命令可以直接基于分支A创建分支C,像这样:

A---B \\ --C

I have tried git rebase A but it does not work.我试过git rebase A但它不起作用。

git rebase --onto A B C

Explanation:解释:

In rebase command, you can define:在 rebase 命令中,您可以定义:

  1. Branch to rebase (by default = HEAD)分支到变基(默认 = HEAD)
  2. The upstream branch of branch-to-rebase branch-to-rebase 的上游分支
  3. Target Branch to rebase to (by default = upstream branch)要变基的目标分支(默认情况下 = 上游分支)

and the command is命令是

git rebase --onto TargetBranchToRebaseTo UpstreamBranch BranchToRebase

In fact you can almost an exact example in git help rebase事实上,你几乎可以在git help rebase一个确切的例子

Here's what I would do:这是我会做的:

git checkout C
git checkout -b rebaseTemp
git rebase -i A

In the interactive rebase, delete all of the commits that correspond to branch B, but aren't a part of branch C (ie the commits that are shared between the two).在交互式 rebase 中,删除与分支 B 对应但不属于分支 C 的所有提交(即在两者之间共享的提交)。 Complete the rebase, then you will have a new branch (rebaseTemp) that contains what I think you want.完成 rebase,然后您将拥有一个包含我认为您想要的内容的新分支 (rebaseTemp)。 You can then merge A into rebaseTemp (which will result in a fast-forward merge).然后您可以将 A 合并到 rebaseTemp (这将导致快进合并)。

A branch visualization tool (ie gitk ) is really useful for this scenario, so you can see what commits are shared.分支可视化工具(即gitk )对于这种情况非常有用,因此您可以查看共享的提交。

Git has a builtin option to rebase that does just that: Git 有一个内置选项来rebase做到这一点:

git checkout C
git rebase --onto A B

This rebases all of the commits after B up to C such that they start on A instead of B.这会将 B 之后的所有提交重新设置为 C,以便它们从 A 而不是 B 开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM