git Git合并并保持分开?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4609841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:54:30  来源:igfitidea点击:

Git merge and keep separate?

git

提问by MidnightLightning

Is there a way to update a side branch with information from another (master or other), and then have the two continue? Like a rebase, but keeping the old data there?

有没有办法用来自另一个(主或其他)的信息更新侧分支,然后让两者继续?就像变基一样,但将旧数据保留在那里?

Original:

原来的:

A---B---C---G---H  master
     \           
      D---E---F  branchA

Result:

结果:

A---B---C---G---H---L  master
     \           \
      D---E---F---J---K  branchA

Such that branchAgets the information from commits C, G, and H, (Commit J is that merge) such that commit K is still a side branch (and future commit L is still on master), but has the updated information from master?

这样branchA从提交 C、G 和 H中获取信息(提交 J 是那个合并),使得提交 K 仍然是一个侧分支(并且未来的提交 L 仍然在 master 上),但是有来自 master 的更新信息吗?

I don't want to do a rebase, because that would end up with:

我不想做一个 rebase,因为那最终会是:

A---B---C---G---H---L  master
                 \
                  D'---E'---F'---K  branchA

Creating "new versions" of D, E, and F as if they happened on top of H instead of B, and the issue is that commits C and E is the renaming of a key folder in the repo, and I want to collapse them together, without merging the other feature updates from branchAjust yet. Rebasing means H uses the new folder name, D' creates the old folder name, and E' removes it again, which isn't the most clean.

创建 D、E 和 F 的“新版本”,就好像它们发生在 H 而不是 B 之上一样,问题是提交 C 和 E 是对 repo 中关键文件夹的重命名,我想折叠它们在一起,而不合并其他功能更新branchA。Rebase 意味着 H 使用新文件夹名称,D' 创建旧文件夹名称,E' 再次删除它,这不是最干净的。

The point is I want to get that folder rename (C and E) in the past and stop bringing it forward. Does that makes sense? Am I looking at this backward? Or should I just deal with the messy rebase "name, rename" trick until the branch gets merged?

关键是我想在过去重命名该文件夹(C 和 E)并停止将其向前推进。这有意义吗?我是在向后看吗?或者我应该在分支合并之前处理凌乱的变基“名称,重命名”技巧?

采纳答案by cdhowie

If your history goes up to H or L on master and F on branchA (that is, J and K do not yet exist), then yes, simply check out branch A and merge:

如果您的历史在 master 上达到 H 或 L,在 branchA 上达到 F(即 J 和 K 尚不存在),那么是的,只需检查分支 A 并合并:

git checkout branchA
git merge H   # Use H's commit identifier if it's not the tip of master

This will merge the changes into branchA and will not disturb the master branch at all.

这会将更改合并到 branchA 中,并且根本不会干扰 master 分支。

If you have already created commit K and you want to insert a merge between it and F, then there's a bit more work to do:

如果你已经创建了提交 K 并且你想在它和 F 之间插入一个合并,那么还有一些工作要做:

git checkout -b temp F   # Create a new branch at commit F
git merge H              # Merge from commit H into the new branch
git cherry-pick K        # Apply the K commit to the merged commit

# And the rest simply replaces the temp branch with branchA
git checkout branchA
git reset --hard temp
git branch -d temp

In both cases, if you later merge in either direction, commit H will be the nearest common ancestor of both history branches, and so future merges will not look past this commit (or past J to F either) when deciding how to merge.

在这两种情况下,如果您稍后在任一方向合并,提交 H 将是两个历史分支的最近共同祖先,因此在决定如何合并时,未来的合并不会越过此提交(或越过 J 到 F)。

回答by H?vard S

A simple git merge masteron branchAshould do (or git merge Hwhere His H's commit-ref if His not the latest on master).

一个简单的git merge masteronbranchA应该做(或者H 的 commit-ref 如果不是最新的 mastergit merge H在哪里)。HH

There will be conflicts if both Cand Erename the same folder which you will have to resolve, but other than that, you should be fine.

会有,如果双方矛盾CE重命名,你将必须解决同一个文件夹,但除此之外,你应该罚款。