简体   繁体   English

如何从 master 的另一个功能分支 rebase 或合并分支

[英]How to rebase or merge a branch from another feature branch from master

So the question is this: Let's imagine I have a master , and I start developing from that state onto a branch A, but then I create a PR from branch A to master , I then start to develop something on top of branch A , because I need some changes from the that branch A , so I create Branch B .所以问题是这样的:假设我有一个master ,我开始从那个 state 开发到一个分支 A ,但是我创建了一个从分支 Amaster的 PR,然后我开始在分支 A之上开发一些东西,因为我需要对该分支 A进行一些更改,因此我创建了Branch B

a -- b -- c  ---- h       <-- Master
           \
           d -- e           <-- Branch A
                 \
                  f -- g     <-- Branch B

This is my current state, then for some reason, I have to make a small change in Branch A, so I proceed to create some commits, and finally merge Branch A into Master.这是我当前的 state,然后由于某种原因,我必须在分支 A 中进行一些小改动,所以我继续创建一些提交,最后将分支 A 合并到 Master。 By then I would need to rebase Branch B in order to get the latest commits from Branch A.What should I do, this would be what I imagine.到那时,我需要重新设置分支 B 以便从分支 A 获取最新的提交。我应该怎么做,这就是我的想象。

a -- b -- c  ---     -- h       <-- Master
           \          /
           d -- e -- i     <-- Branch A
                      \
                       f -- g     <-- Branch B

When you've added commit i to your Branch-A you end up with:当您将 commit i添加到您的Branch-A时,您最终会得到:

a -- b -- c ------ h       <-- Master
           \
            d -- e -- i     <-- Branch-A
             \
              f -- g     <-- Branch-B

(Remember, your original drawing includes commit h so it must be there before the merge happens.) Making the actual merge produces a new commit j : (请记住,您的原始绘图包括提交h因此它必须在合并发生之前存在。)进行实际合并会产生一个新的提交j

a -- b -- c ------ h -- j       <-- Master
           \           /
            d -- e -- i     <-- Branch-A
             \
              f -- g     <-- Branch-B

This merge is required because existing commit h must be preserved, while new commits dei become reachable from the tip of master : this requires a merge commit j whose parents are the two existing commits h and i .此合并是必需的,因为必须保留现有提交h ,而新提交dei可以从master的尖端到达:这需要合并提交j ,其父级是两个现有提交hi (If h did not exist, a fast-forward operation would be possible: that changes the resulting graph shape, so its effect ripples through the rest of this answer. Let's assume that h does exist, and/or you force a non-fast-forward merge anyway.) (如果h不存在,则可以进行快进操作:这会改变生成的图形形状,因此其效果会通过此答案的 rest 产生涟漪。假设h确实存在,和/或您强制执行非快速- 无论如何向前合并。)

At this point you now have the option of deleting the name Branch-A entirely (at any time), and also the option of copying existing commits f and g to new-and-improved commits f' and g' .此时,您现在可以选择完全删除名称Branch-A (在任何时候),还可以选择将现有提交fg复制到新的和改进的提交f'g' That's the rebase you're asking about.这就是你要问的变基。

Should you rebase them?应该重新设置它们吗? That's a harder question.这是一个更难的问题。 But, provided everyone else agrees that original commits f and g can be abandoned in favor of the new-and-improved f' and g' , it's fine.但是,只要其他人都同意可以放弃原始提交fg以支持新的和改进f'g' ,那很好。 It might also make things prettier later.它也可能在以后使事情变得更漂亮。 If you decide not to rebase them, the question of how to do it is moot.如果你决定不重新设置它们,那么如何去做的问题是没有实际意义的。

If you do decide to rebase them, your next question needs to be: upon which target commit?如果您确实决定对它们进行变基,那么您的下一个问题需要是:基于哪个目标提交? Should f' come after i , or should it come after j ? f'应该在i之后,还是应该在j之后? Let's draw both scenarios:让我们画出这两种情况:

(option 1: after i)

a -- b -- c ------ h -- j       <-- Master
           \           /
            d -- e -- i     <-- Branch-A
             \         \
              \         f' -- g'   <-- Branch-B
               \
                f -- g   [abandoned]

(option 2: after j)

                          f' -- g'   <-- Branch-B
                         /
a -- b -- c ------ h -- j       <-- Master
           \           /
            d -- e -- i
             \
              f -- g  [abandoned]

To make option 2 happen, run:要使选项 2 发生,请运行:

git checkout Branch-B
git rebase Master

(I prefer to use two separate commands here, but you can spell this git rebase Master Branch-B if you like: the extra Branch-B argument makes git rebase start by running git checkout first.) Note that I've deleted the name Branch-A : we didn't need it here. (我更喜欢在这里使用两个单独的命令,但是如果你愿意,你可以拼写这个git rebase Master Branch-B :额外的Branch-B参数使git rebase通过运行git checkout的名称。) Branch-A :我们这里不需要它。 You may or may not have deleted it at this point, and if not, you might delete it later.此时您可能已经删除它,也可能没有,如果没有,您可以稍后将其删除。

To make option 1 happen, run:要使选项 1 发生,请运行:

git checkout Branch-B
git rebase Branch-A

This time we need the name Branch-A to easily find the hash ID of commit j .这次我们需要名称Branch-A来轻松找到提交j的 hash ID。 So if you want to make option 1 happen, and don't want to have to run git log and cut and paste hash IDs, keep the name Branch-A around at least until this point.因此,如果您想让选项 1 发生,并且不想运行git log并剪切和粘贴 hash ID,请至少在此之前保留名称Branch-A After the rebase finishes, we don't need the name Branch-A any more and now it's safe to delete.变基完成后,我们不再需要名称Branch-A ,现在可以安全地删除。

Technically , we didn't need it before the rebase either, because we could look up the hash ID of j any time: git log Master starts at j and shows us that, then shows us one of h or i , and eventually we see i and therefore see its hash ID.从技术上讲,我们在 rebase 之前也不需要它,因为我们可以随时查找j的 hash ID: git log Masterj开始并向我们展示,然后向我们展示hi之一,最终我们看到i因此看到它的 hash ID。 So we can find it the hard way no matter what—but if you keep the name for a little while, we don't have to do that.所以无论如何我们都可以找到它的艰难方式——但如果你保留这个名字一段时间,我们就不必这样做。

Note: If you use GitHub's "rebase and merge" or "squash and merge" clicky buttons, those do not do a standard merge.注意:如果您使用 GitHub 的“rebase and merge”或“squash and merge”clicky 按钮,这些按钮不会执行标准合并。 Instead, they copy commits to new ones, similar to rebase.相反,他们将提交复制到新的提交,类似于 rebase。 That is, they'll make new and supposedly improved copies of the commits you're trying to incorporate.也就是说,他们将为您尝试合并的提交制作新的和据称改进的副本。 In that case none of your own existing names find the correct commit hash IDs any more because all of your names still remember your original commits, with their hash IDs, not the copies / new-commits that GitHub will have made.在这种情况下,您自己的现有名称都找不到正确的提交 hash ID ,因为您的所有名称仍然记得您的原始提交,以及它们的hash ID,而不是 ZD3B7C913CD04EBFECZ8 将进行的副本/新提交。 So if you do use GitHub's fancier operations, you will have to run git fetch to get the new commits from GitHub, then use other names, or raw commit hash IDs;因此,如果您确实使用 GitHub 更高级的操作,则必须运行git fetchGitHub 获取新提交,然后使用其他名称,或原始提交 Z0800FC577294C34E0B28AD283943594Z you should ditch, or fix up, your own branch names soon, to avoid getting confused as to which commits were the originals and which were the new, and supposedly improved, copies.您应该尽快放弃或修复您自己的分支名称,以避免混淆哪些提交是原始提交,哪些是新的,据说是改进的副本。 (Is commit f9131d9 the original, or is it 70abf32c ? What about a31cf2a vs 0449acd ? The hash IDs look totally random and keeping them straight is... not fun.) (提交f9131d9是原始版本,还是70abf32ca31cf2a0449acd怎么样?hash ID 看起来完全随机,保持它们直接是……不好玩。)

This approach is "overbranched".这种方法是“过度分支的”。 Just develop new features related to branch A inside it.只需在其中开发与分支 A 相关的新功能。 Don't create branch B. If new features goes to master - commit branch A and merge to master.不要创建分支 B。如果新功能转到 master - 提交分支 A 并合并到 master。

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

相关问题 从另一个分支重新设置基础,然后在合并另一个分支后合并到master - Rebase from another branch and then merge into master after merged the other branch 将分支变基或合并到合并到 master 中的另一个功能分支上 - Rebase or merge a branch onto another feature branch merged in master Git从master迁移到Feature分支问题 - Git rebase from master to feature branch issue 我如何一次性从远程拉到主机并从主机转换到我的功能分支? - How do I pull and rebase from remote to master and rebase from master to my feature branch in one shot? 有没有更简单的方法来git pull master,git rebase,git从功能分支合并到master中? - Is there an easier way to git pull master, git rebase, git merge into master from feature branch? 合并来自master中另一个分支的分支 - Merge a branch from another branch in master 如何从Git中的功能分支将上游/母版合并到原始/母版中? - How to merge upstream/master into origin/master from a feature branch in Git? 如何将主题分支的功能分支重新定义为开发分支? - How to rebase feature branch from theme branch to development branch? 从另一个从master重新分支的分支重新启动分支 - Rebase a branch from another branch that is rebased from master 将主站重新设置为功能分支 - Rebase master to feature branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM