【问题标题】:Apply difference between two branches on a third branch在第三个分支上应用两个分支之间的差异
【发布时间】:2022-08-15 14:31:11
【问题描述】:

我有这些分支:

  • 主要
  • 功能库(基于主)
  • feature-foo(基于功能库)

分支 \"feature-base\" 包含许多不再需要的提交。

我想创建一个新分支 \"only-foo\",其中包含 \"feature-base\" 和 \"feature-foo\" 之间的区别。

我想过这个解决方案

git checkout feature
git diff feature-foo > ~/tmp/foo.patch
git switch main
git switch -c only-foo
patch -p0 < ~/tmp/foo.patch

但这不包括二进制文件。

有没有更好的方法将两个分支的差异应用到第三个分支?

仅获取更改就可以了。如果来自 \"feature-foo\" 的提交消息(和其他元数据)丢失也没关系。

  • 我建议查看git cherry-pick 和git rebase。以后有时间我会写一个更详细的答案。

标签: git


【解决方案1】:

应用“补丁”总是很棘手,因为 git 缺少补丁来自的部分的上下文(它在进行樱桃采摘等时具有)。

我会这样做。假设场景是我想将git diff A..B 的结果应用到 C.我会这样做:

git checkout -b temp B
git reset --soft A # put all differences between A and B in index ready to commit
git commit -m "Single commit for A..B"
git checkout C
git cherry-pick temp

那应该这样做。

【讨论】:

    【解决方案2】:

    首先创建only-foo 分支作为feature-foo 的副本:

    git branch only-foo feature-foo
    

    然后使用 git rebase 和 --onto 选项并将其重新设置为 main:

    git rebase --onto main feature-base only-foo
    

    这使得main 成为新的基础,将来自only-foo 的所有提交应用到它,并将feature-base 作为旧的基础。

    请注意,如果您以后不再需要该分支,则可以跳过创建 only-foo 分支并直接变基 feature-foo。

    另请参阅 git 教程的 Advanced rebase application 部分,了解有关变基的更多详细信息。

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 2021-03-13
      • 2021-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      相关资源
      最近更新 更多