gpt4 book ai didi

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

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

我有我的 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/

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