将基于主题分支的分支的更改合并到 git 中的不同主题分支

发布于 2024-11-03 08:02:43 字数 843 浏览 6 评论 0原文

我的团队正在 git 中开发一个共享主题分支,我将其称为“topic1”。我正在对由 topic1 组成的分支上的一些代码进行重构,我将其称为“重构”。我定期将 topic1 合并到 refactor 中,以便能够及时了解更改,但尚未将 refactor 合并回 topic1 中,因为重构仍在进行中。

还有另一个主题分支,我将其称为“topic2”,它是最近从 master 创建的。我想做的就是将我对“重构”所做的更改合并到由 topic2 组成的新分支,我将其称为“topic2_refactor”。 (即提交中的更改只能通过重构访问,而不能通过 topic1 访问。)

我知道如何查看这些更改:

git log origin/refactor --not origin/topic1

所以我想做的是这样的 - 但这个语法不正确:

git checkout topic2
git checkout -b topic2_refactor

然后这个:

git merge origin/refactor --not origin/topic1

或者这样:(

 git cherry-pick origin/refactor --not origin/topic1

由于 master 上发生了一些更改,这些更改后来被合并回重构分支,上述似乎导致了不必要的合并冲突。)

我希望有一种干净的方法来做到这一点并避免不必要的合并冲突,这些冲突稍后在“重构”分支的历史中解决。使用 git rebase、git filter-branch 等可以实现这一点吗?

My team is working on a shared topic branch in git which I will call "topic1." I was working on a refactor of some code on a branch made off of topic1, which I will call "refactor." I have been periodically merging topic1 into refactor so I can stay up to date with changes, but have not merged refactor back into topic1 because the refactor is still in progress.

There is another topic branch, which I'll call "topic2" which was recently created off of master. What I'd like to do is merge only the changes that I've made on "refactor" to a new branch made off of topic2, which I'll call "topic2_refactor." (I.E. the changes in the commits accessible only by refactor but not by topic1.)

I know how to see these just these changes:

git log origin/refactor --not origin/topic1

So what I'd like to do is something like this - but this syntax is not correct:

git checkout topic2
git checkout -b topic2_refactor

And then this:

git merge origin/refactor --not origin/topic1

Or this:

 git cherry-pick origin/refactor --not origin/topic1

(The above seems to be causing merge conflicts that aren't neccessary, due to some changes which occured on master that was later merged back into the refactor branch.)

I was hoping that there is a clean way to do this and avoid unnecessary merge conflicts that were resolved later on in the history of the "refactor" branch. Might this be possible using git rebase, git filter-branch, etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天 2024-11-10 08:02:43

您可以尝试使用 --onto 选项进行 git rebase。这允许变基操作从基于“topic1”的“refactor”分支获取需要应用的差异,然后将它们应用到“topic2”

git co refactor
git co -b topic2_refactor
git rebase --onto topic2 topic1 # bases the diffs off of topic1, but applies them to topic2

。您的成功可能会有所不同。仍然可能会发生有问题的冲突。这样做的缺点是,您现在将拥有两个具有相同更改的独立重构分支,但由于这是一个变基,它们具有不同的历史记录,如果您不小心,它们很容易出现分歧(您必须不断地从一个分支中挑选)分支到另一个或类似的东西)。

然后,当 topic1 和 topic2(重构后)需要再次合并到 master 时,您可能会遇到麻烦,因为它们将具有所有相同的重构提交。尽管 git 通常在这方面做得很好。

由于重构似乎独立于这些主题分支,因此我会考虑将重构分支重新定基为 master,这样您就可以在重构完成后将这些更改合并到两个主题中。

You can try git rebase with the --onto option. What this allows is for the rebase operation to grab the diffs it needs to apply from your 'refactor' branch, based off 'topic1', but then apply them to 'topic2'

git co refactor
git co -b topic2_refactor
git rebase --onto topic2 topic1 # bases the diffs off of topic1, but applies them to topic2

Your success may vary. There could still be problematic conflicts that occur. The downside to this is you'll now have two separate refactor branches with the same changes, but since this was a rebase they have different histories that can easily diverge if you're not careful (you'd have to constantly cherry pick from one branch to the other or something similar).

Then you could run into trouble when both topic1 and topic2 (after refactoring) need to be merged into master again, since they'll have all those identical refactor commits. Though git is usually pretty good with that.

Since it seems the refactoring is independent of these topic branches, I'd consider rebasing the refactor branch off of master, so you can merge those changes into both topics when the refactoring is finished.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文