择优挑选与变基

发布于 2024-09-04 20:55:00 字数 386 浏览 9 评论 0原文

以下是我经常遇到的场景:

您在 masterdesign 上有一组提交,我想将其放在 生产 之上分支。

我倾向于创建一个以 生产 为基础的新分支,在其上挑选这些提交并将其合并到 生产

然后当我合并 master 时在生产中,我面临合并冲突,因为即使更改是相同的,但由于樱桃选择而被注册为不同的提交。

我找到了一些解决方法来解决这个问题,所有这些方法都很费力,可以称为“黑客”。

尽管我没有做太多的变基,但我相信这也会创建一个新的提交哈希。

我应该在我挑选的地方使用变基吗?与此相比,它还有哪些优点呢?

The following is a scenario I commonly face:

You have a set of commits on master or design, that I want to put on top of production branch.

I tend to create a new branch with the base as production cherry-pick these commits on it and merge it to production

Then when I merge master to production, I face merge conflicts because even tho the changes are same, but are registered as a different commit because of cherry-pick.

I have found some workarounds to deal with this, all of which are laborious and can be termed "hacks".

Altho' I haven't done too much rebasing, I believe that too creates a new commit hash.

Should I be using rebasing where I am cherrypicking. What other advantages does that have over this.

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

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

发布评论

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

评论(1

仅冇旳回忆 2024-09-11 20:55:00

您应该在生产之上创建一个 rebase --interactive 设计分支,其中:

  • 您可以重新排序您的提交,从生产中需要的那些开始
  • ,然后可以将生产合并到设计的最后提交您想要合并(快进)
    -x--x--x1--x--x2 (design)
      \
       p--p (production)

需要将 x1 和 x2 包含在生产中:

git checkout design
git rebase --interactive production

-x
  \
   p--p (production)
       \ 
        x1'-x2'--x'--x' (design)

然后:

git checkout production
git merge x2'
-x
  \
   p--p--x1'--x2' (production)
                \
                 x'--x' (design)

这允许您:

  • 根据生产提交验证当前设计提交(在变基期间)
  • 重新排序设计提交
  • ,包括生产中的第一个
  • 允许随后从设计到生产的合并成为一个快进的过程。

Lakshman Prasad 添加:

大多数时候我都会在一天结束时推送更改。所以并没有多大帮助。对于推送的主分支,你的答案会如何变化

我会做同样的事情,但是使用仅为操作创建的私有分支:

git checkout master
git checkout -b master-private
    -x--x--x1--x--x2 (master, master-private)
      \
       p--p (production)

,然后是变基,这次使用私有分支(即你永远不会推送的分支)。

git rebase --interactive production

-x--x--x1--x--x2 (master)
  \
   p--p (production)
       \ 
        x1'-x2'--x'--x' (master-private)

那么:

git checkout production
git merge x2'

-x--x--x1--x--x2 (master)
  \
   p--p--x1'--x2' (production)
                \
                 x'--x' (master-private)

master 不会从提交重新排序中受益(具有更符合逻辑的顺序),但至少您可以随时推送 master 。

并且生产仍然可以准确地包含它所需要的内容。
如果后续对 master 的提交有相同的问题(有些需要包含在 生产 中,其他的稍后会包含在内),我会:

  • 删除 master-private > (我们并不真正关心那些 x',来自 master 的 x 提交的副本)
  • 在 master 之上创建一个 master-private 分支,
  • 重新执行 rebase --interactive code>,采用粗略的冲突解决策略(除了该 master-private 分支的第一次提交,因为这些提交需要集成到 生产 分支中)
    -x--x--x1--x--x2--x--x3--x (master)
      \
       p--p--x1'--x2'--x3' (production)
                   |     \
                   |      x''--x'' (master-private)
                    \
                     x'..x' (old x' from the first master-private)

You should make a rebase --interactive your design branch on top of production, where:

  • you can reorder your commits, starting with the ones you need in production
  • you can then merge production to the last commit of design you want to incorporate (fast forward)
    -x--x--x1--x--x2 (design)
      \
       p--p (production)

With x1 and x2 needing to be included in production:

git checkout design
git rebase --interactive production

-x
  \
   p--p (production)
       \ 
        x1'-x2'--x'--x' (design)

Then:

git checkout production
git merge x2'
-x
  \
   p--p--x1'--x2' (production)
                \
                 x'--x' (design)

That allows you:

  • to validate current design commits against the production commits (durung the rebase)
  • reorder the design commits
  • include the first ones in production
  • allows subsequent merges from design to production to be a fast-forward one.

Lakshman Prasad adds:

I push the changes at the end of the day most of the time. So doesn't really help that much. How would your answer change for the pushed master branch

I would do the same, but with a private branch created just for the operation:

git checkout master
git checkout -b master-private
    -x--x--x1--x--x2 (master, master-private)
      \
       p--p (production)

, then the rebase, this time with the private branch (i.e. a branch you won't ever push).

git rebase --interactive production

-x--x--x1--x--x2 (master)
  \
   p--p (production)
       \ 
        x1'-x2'--x'--x' (master-private)

Then:

git checkout production
git merge x2'

-x--x--x1--x--x2 (master)
  \
   p--p--x1'--x2' (production)
                \
                 x'--x' (master-private)

master won't benefit from the commit reordering (with a more logical order), but at least you can push master whenever you want.

And production can still include exactly what it needs.
If subsequent commits to master have the same issue (some need to be included to production, other will later), I would:

  • delete master-private (we don't really care about those x', copy of x commits from master)
  • make a master-private branch on top of master
  • re-do the rebase --interactive, with a crude conflict resolution tactic (except for the first commits of that master-private branch, since those ones need to be integrated in the production branch)
    -x--x--x1--x--x2--x--x3--x (master)
      \
       p--p--x1'--x2'--x3' (production)
                   |     \
                   |      x''--x'' (master-private)
                    \
                     x'..x' (old x' from the first master-private)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文