简体   繁体   English

将提交从一个分支变基到另一个分支

[英]Rebasing a commit from a branch to another branch

I have a question regarding git rebasing.我有一个关于 git 变基的问题。 There are tons of threads out there and I am not sure which one works with my usecase and I don't want to mess the remote branches since I am a beginner.那里有很多线程,我不确定哪一个适用于我的用例,而且我不想弄乱远程分支,因为我是初学者。

From what I've seen, I can use the commands:据我所见,我可以使用以下命令:

  • git branch to create or change the branch git 分支创建或更改分支
  • git rebase -i to rebase interactively git rebase -i 以交互方式变基
  • git rebase <branch_name> git 变基 <branch_name>
  • git checkout to switch to the branch git结账切换到分店

Here is my case:这是我的情况:

I have a branch branch_A that is on remote: branch_A我有一个远程分支 branch_A:branch_A

Then, branch_A diverged to a branch branch_B and a branch branch_C (multiple commits)然后,branch_A 分叉到一个分支 branch_B 和一个分支 branch_C(多次提交)

branch_A - branch_B分支_A - 分支_B

branch_A - branch_C分支_A - 分支_C

Then, I did 3 commits (D1, D2 and D3) on branch B that I call branch branch_D:然后,我在分支 B 上做了 3 次提交(D1、D2 和 D3),我称之为分支 branch_D:

branch_A - branch_B - D1 - D2 - D3 (branch_D)分支_A - 分支_B - D1 - D2 - D3(分支_D)

branch_A - branch_C分支_A - 分支_C

I pushed these to remote so this state is on remote.我将这些推到远程,所以这个 state 在远程。

Now I want to move all the 3 commits from B - D to C - D to get:现在我想将所有 3 个提交从 B - D 移动到 C - D 以获得:

branch_A - branch_B分支_A - 分支_B

branch_A - branch_C - D1 - D2 - D3 - branch_D分支_A - 分支_C - D1 - D2 - D3 - 分支_D

Note: The changes are minor (no conflicts can occur) so I could just make a patch and apply it but I want to improve my level at git.注意:更改很小(不会发生冲突),所以我可以打一个补丁并应用它,但我想提高我在 git 的水平。

What I fear is that I accidentally delete A, B, C or D (although I think deleting branch_D is a possibility but I know I can push force).我担心的是我不小心删除了 A、B、C 或 D(虽然我认为删除 branch_D 是可能的,但我知道我可以推动力量)。 I know I can use gitk --all without pushing to prevent things from getting bad.我知道我可以使用 gitk --all 而不用推动来防止事情变糟。

I tried several things with git rebase and git rebase -i but it didn't seem to work.我用 git rebase 和 git rebase -i 尝试了几件事,但它似乎没有用。 Also I know I could git merge, but this is not what I want in this usecase.我也知道我可以 git 合并,但这不是我在这个用例中想要的。

A few key concepts:几个关键概念:

  • Commits point to their parents.提交指向他们的父母。
  • Branches point to a single commit at a time.分支一次指向一个提交。 When we say something is "on a branch", we actually mean "reachable by following parents from that branch pointer".当我们说某事“在一个分支上”时,我们实际上的意思是“通过从那个分支指针跟随父级可以到达”。
  • Commits are immutable.提交是不可变的。 When we talk about "moving" or "amending" commits, we actually mean "creating new, similar, commits".当我们谈论“移动”或“修改”提交时,我们实际上是指“创建新的、类似的提交”。
  • Branches are independent - changing branch A has no immediate effect on branch B, or even on a branch called A on a server.分支是独立的 - 更改分支 A 不会立即影响分支 B,甚至不会影响服务器上名为 A 的分支。

So, as I understand it, your commit graph looks like this:因此,据我了解,您的提交图如下所示:

      A        B       D
      |        v       v
      |  <-x <-x <-x <-x
      v /
... <-x  
        \
         <-x <-x
               ^
               C

What you want to create is this:您要创建的是这样的:

      A        B
      |        v
      |  <-x <-x
      v /
... <-x  
        \
         <-x <-x <-x <-x
               ^       ^
               C       D

Where the two new commits pointed at by D have been recreated with a parent of C rather than B. You don't need to touch branches A, B, and C; D 指向的两个新提交已使用 C 而不是 B 的父级重新创建。您不需要触摸分支 A、B 和 C; you just need to rebase branch D.你只需要重新设置分支 D。

Until you're confident, you might want to create a backup of the current branch D, in case things go wrong:在您有信心之前,您可能需要创建当前分支 D 的备份,以防 go 错误:

git branch D_backup D

Then, the version of the git rebase command I find easiest to understand is "git rebase old_parent up_to_old_commit --onto new_parent", so:然后,我发现最容易理解的git rebase命令的版本是“git rebase old_parent up_to_old_commit --onto new_parent”,所以:

git rebase B D --onto C

That should leave you with:那应该给你留下:

      A        B       D_backup
      |        v       v
      |  <-x <-x <-x <-x
      v /
... <-x  
        \
         <-x <-x <-x <-x
               ^       ^
               C       D

If it goes wrong, you can replace D with D_backup:如果出错,可以将 D 替换为 D_backup:

git branch -D D
git branch D D_backup

If it all looks fine, delete the backup, and overwrite the remote copy of branch D:如果一切正常,删除备份,并覆盖分支 D 的远程副本:

git branch -D D_backup
git push --force-with-lease origin D

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

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