git - 使用 git merge 两个非常不同的分支?

标签 git

我有我的 master 分支和我的 verydifferentbranch 它们有相同的祖先......大约 300 次提交前。现在 verydifferentbranch 的功能已经完成,我想把它放在主 branch 下。进行 rebase 会导致每个补丁都有很多 merge 冲突,以至于它本身就是一个大项目来解决所有冲突。

除了强制将 verydifferentbranch 的头部 push master 分支之外,我不知道该怎么做。这样做我会失去我所有的历史,而这不是我真正想做的事情。

我还有哪些其他选择?

最佳答案

听起来你的历史是这样的:

...---o---A---o---...---o---o---B   master
           \
            o---o---...---o---C     verydifferentbranch

你说你担心强推会丢失历史verydifferentbranchmaster .这样的操作实际上会丢弃 A 之后的所有内容直到 B .

您可以通过 merge 来保留历史记录,或者只是在废弃的分支尖端放置一个标签并使其保持未 merge 状态。

使用 merge

merge 将允许您保留双方的历史记录:

...---o---A---o---...---o---o---B
           \                     \
            o---o---...---o---C---M  master

您执行的 merge 类型将决定为提交 M 创建的内容。正常 merge (使用 recursive merge 策略)听起来会在您的案例中产生大量冲突。 如果您确实需要 merge 来自 A..B 的更改提交时,除了解决由 merge 或 rebase 所带来的冲突外别无他法。它们出现了。)但是,如果你只想 MC 具有相同的内容(即你想忽略由 A..B 提交表示的更改),然后你可以使用 ours merge 策略。

git-merge(1)描述了 ours merge 策略:

This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.

您可以使用 Merge commit 'abandoned/foo-features' 的消息生成 M像这样:

git tag -m 'describe reason for abandonment here...' \
    abandoned/foo-features master                   # tag abandoned branch tip
git checkout verydifferentbranch                    # checkout dominant branch
git branch -M verydifferentbranch master            # replace master
git merge -s ours abandoned/foo-features            # merge only other's history
git tag -d abandoned/foo-features                   # optional: delete the tag
git push wherever master tag abandoned/foo-features # publish them

这些命令的变体将为您提供略有不同的 M 自动提交消息(您始终可以提供自己的 git merge -m 或之后用 git commit --amend 进行修改)。

Gist 是生成的 master将有两条历史记录,但与原始 master 没有任何变化侧(这些更改仍在历史记录中,它们只是未在提交 M 引用的树中表示)。

让它悬着

如果“重写”是可以接受的master (即没有基于任何提交的其他工作 A..B ,或者所涉及的用户不介意它将对 recover from the rewrite 进行的工作),那么您可以在 master 的当前提示处留下一个标签。并替换 masterverydifferentbranch .

...---o---A---o---...---o---o---B   (tag: abandoned/foo-features)
           \
            o---o---...---o---C      master

这样安排:

git tag -m 'describe reason for abandonment here...' \
    abandoned/foo-features master                    # tag abandoned branch tip
git branch -M verydifferentbranch master             # replace master
git push wherever +master tag abandoned/foo-features # publish them

关于git - 使用 git merge 两个非常不同的分支?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5956300/

相关文章:

bash - 通过命令行检查 BitBucket 存储库是否存在

git - 如何在 github 中比较/区分 master 和 fork repo 的特定版本

git - 获取要由特定 git commit 命令提交的文件列表

git - 在 docker 容器中获取代理后面的 git

git - 写库的时候,是不是应该把TypeScript生成的JavaScript文件放到gitignore里面?

git - Cherry pick 仅提交涉及特定文件的内容

java - 在 IntelliJ 的 Kotlin > Java 转换后将源代码恢复为 Java

javascript - meteor JS : Installing on apache/linux server

Git:忽略版本控制文件

linux - git --- 如何在 linux 上安排 `git push`?