gpt4 book ai didi

git - 如何在git中 merge 提交之前找到两个分支的共同祖先?

转载 作者:太空狗 更新时间:2023-10-29 13:59:56 24 4
gpt4 key购买 nike

背景

我正在尝试编写一个脚本来使用 git 简化我的向后移植过程。

在当前版本中修复错误的一般过程是这样的:

  1. master 分支到错误修复分支,例如错误修正/abc
  2. 进行所有提交以修复 bugfix/abc 上的错误
  3. bugfix/abc merge 到 master(没有快进 merge )

现在,当我想将修复程序反向移植到版本 1(分支 v1)时,我会这样做:

  1. bugfix/abc 创建一个新分支,例如错误修复/def
  2. 然后在 master 上手动查找 merge 提交之前的提交,例如f4d399238f
  3. 现在我使用 rebase:$ git rebase --onto v1 f4d399238f bugfix/def

这很好用(在发现我必须在上游 merge 之前使用提交之后)。

问题

如何在 merge 提交之前找到两个分支的共同祖先? (用于向后移植过程的第 2 步)。

尝试过

  1. git merge-base bugfix/abc master
    • 因为 merge 已经完成,所以只返回 bugfix/abc
    • 开头的提交
  2. 使用 git log merge #1 的结果以获取此提交的子项

更新

这个问题和Find common ancestor of two branches之间的关键区别是两个分支已经 merge 了。正如我提到的,最佳提交 作为 merge 分支的头返回。我需要共同的祖先在 merge 提交之前

假设bug被修复并 merge 到master后的分支是这样的:

A---B v1
\
C---D---F---H master
\ /
E---G bugfix/abc

运行 $ git merge-base master bugfix/abc 将返回 G 但我需要得到 D (甚至 F 会为了使用 rebase --onto 的目的而做)。

一旦我得到D,我就会运行:

$ git branch bugfix/def bugfix/abc
$ git rebase --onto v1 D bugfix/def
$ git checkout v1
$ git merge bugfix/def

最终得到想要的结果:

      E'---G' bugfix/def
/ \
A---B--------I v1
\
C---D---F---H master
\ /
E---G bugfix/abc

最佳答案

How do I find the common ancestor of two branches before a merge commit? (for step 2 of the backport process).

有几个选项可供使用:

git merge 库

git merge-base是你要找的命令

git merge-base finds best common ancestor(s) between two commits to use in a three-way merge. One common ancestor is better than another common ancestor if the latter is an ancestor of the former. A common ancestor that does not have any better common ancestor is a best common ancestor, i.e. a merge base. Note that there can be more than one merge base for a pair of commits.

从日志中手动查找

git log --decorate --graph --oneline --all

这将在日志中显示 fork 点,以便您可以跟踪分支的提交 ID。

关于git - 如何在git中 merge 提交之前找到两个分支的共同祖先?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com