如何从一个不同的主题分支对一个主题分支进行 Git 变基?

huangapple go评论48阅读模式
英文:

How to git rebase a topic branch from a different topic branch?

问题

我正在寻找一系列 git 命令来获取尝试执行类似于我的更改的更改序列,但不是从主分支获取,而是从存储库的另一个贡献者的分支获取。

我通常会使用以下命令,如果从主分支进行 rebase:

git checkout TopicA
git rebase master
git push origin TopicA
git push --set-upstream origin TopicA

目前存储库的设置如下:

                F"---G"---H"---I TopicB
                |
A---B---C---D---E  master
                |
                F'---G'---H'---J   TopicA

我需要用 TopicB 的更改替换 TopicA 的更改。

ETA:所需的布局是:

                F"---G"---H"---I TopicB
                |
A---B---C---D--E  master
                |
                F"---G"---H"---J  TopicA

我本能地认为在我的序列中将 'master' 替换为 'TopicB',但这并不是我从 rebase 文档中得到的印象。

我的另一个想法是创建一个新的分支(TopicC),从 TopicB 派生,然后删除 TopicA,然后将 TopicC 重命名为 TopicA。

我不喜欢后一种方法,因为我需要将 TopicA 中的一些更改调整到 TopicB 中的更改中。

到目前为止,我已经阅读了 git-rebase(1) 手册页面,但有点困惑和不解,并且不想尝试太多可能会损坏存储库、我的 TopicA 分支或 TopicB 分支的事情。

我在想是否

git rebase --onto TopicB

就是我需要的一切,尤其是在审阅了 https://stackoverflow.com/questions/5748930/merging-changes-from-a-branch-based-off-a-topic-branch-to-a-different-topic-bran 之后,尽管我有点不清楚那种情况和我的情况是否相同。

另一个建议的问题:https://stackoverflow.com/questions/73379567/git-rebase-to-a-different-branch-while-excluding-a-certain-branch 似乎不太有帮助。

我使用 Git Bash 处理我的 git 命令。我也可以使用 TortoiseGit,但 Git Bash 是我首选的工具。

英文:

I'm looking for a sequence of git commands to acquire changes that attempt to do something similar to my changes, but not from master/main, but from another contributor to the repo's branch.

I would normally use the following if rebasing from master:

git checkout TopicA
git rebase master
git push origin TopicA
git push --set-upstream origin TopicA

The setup for the repo is currently:

                F"---G"---H"---I TopicB
                |
A---B---C---D---E  master
                |
                F'---G'---H'---J   TopicA

I need to replace the o' changes of TopicA with the o" changes of TopicB.

ETA: So the desired layout is:

               F"---G"---H"---I TopicB
               |
A---B---C---D--E  master
               |
               F"---G"---H"---J  TopicA
                               

My instinct is to replace 'master' with 'TopicB' in my sequence, but that was not the impression I got from the documentation on rebase.

My other thought would be to make a new branch (TopicC) forked off of TopicB, and then delete TopicA and then rename TopicC to TopicA.

I don't like the latter approach because I need to adapt some changes from TopicA into the changes that are present in TopicB.

So far I've read through the git-rebase(1) Man Page, and am left a bit confused and perplexed by it, and don't want to try too many things that could damage the repo or either my TopicA branch or the TopicB branch.

I'm wondering if

git rebase--onto TopicB 

will be all I'd need, especially after reviewing https://stackoverflow.com/questions/5748930/merging-changes-from-a-branch-based-off-a-topic-branch-to-a-different-topic-bran though I'm a little unclear whether that case and my case are the same.

The other suggested question: https://stackoverflow.com/questions/73379567/git-rebase-to-a-different-branch-while-excluding-a-certain-branch did not seem helpful.

I'm using Git Bash for handling my git commands. I also have TortoiseGit available, but Git Bash is my preferred tool.

答案1

得分: 2

如果您希望主题A与主题B至少在此刻完全相同,那么将它们变为相同的提交。假设主题A已经存在:

git switch topicA
git reset --hard topicB

这将删除主题A上已有的所有提交,并从主题B现在的位置重新开始主题A。

英文:

If you wish topic A were, for the moment at least, absolutely identical to topic B, then make them the same commit. Assuming that topicA already exists:

git switch topicA
git reset --hard topicB

That will drop all the commits already on topic A and start topic A all over again where topic B is now.

答案2

得分: 0

Your proposed command

git rebase --onto TopicB

would result in

A---B---C---D---E---F"---G"---H"---I---F'---G'---H'---J TopicA

You want

git rebase --onto H" H'

H" being the last commit you want to take from TopicB and H' being the last commit you don't want from TopicA.
Run this while on the TopicA branch. You can get the commit ids for H" and H' from TortoiseGit as explained by this answer.

Note that since you're concerned about damaging your repo you could first create a new branch from TopicA

git branch TopicA-backup

Then if you decide you don't like the state TopicA ends up in you can restore it with

git reset TopicA-backup

and delete the backup branch with

git branch -d TopicA-backup

For more detail on the syntax of the rebase command have a look at Matt's answer here.

英文:

Your proposed command

git rebase --onto TopicB 

would result in

A---B---C---D---E---F"---G"---H"---I---F'---G'---H'---J TopicA

You want

git rebase --onto H" H'

H" being the last commit you want to take from TopicB and H' being the last commit you don't want from TopicA.
Run this while on the TopicA branch. You can get the commit ids for H" and H' from TortoiseGit as explained by this answer.


Note that since you're concerned about damaging your repo you could first create a new branch from TopicA

git branch TopicA-backup

Then if you decide you don't like the state TopicA ends up in you can restore it with

git reset TopicA-backup

and delete the backup branch with

git branch -d TopicA-backup

For more detail on the syntax of the rebase command have a look at Matt's answer here.

huangapple
  • 本文由 发表于 2023年2月7日 02:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定