简体   繁体   English

git 变基浮动分支

[英]git rebase a floating branch

I have imported another repo as a branch based on solutions here .我在这里基于解决方案导入了另一个 repo 作为分支。

Now, the new imported branch is floated while it should be connect the way specified in the image.现在,新导入的分支是浮动的,它应该按照图像中指定的方式连接。

How should I do the rebase?我应该如何做rebase?

git checkout imported-branch
git rebase ????? --onto ????

git rebase

You can't get what you've drawn, because you drew in commit hash IDs: f8114a for instance.你不能得到你画的东西,因为你在提交 hash IDs: f8114a中画了。 Given that f8114a is drawn as a root commit, this means that f8114a is always a root commit, in the past, present, and forever in the future too.鉴于f8114a被绘制为根提交,这意味着f8114a在过去、现在和将来永远都是根提交。 But in the right hand side of the drawing, f8114a is not a root commit: it has a parent d0ece .但在图的右侧, f8114a不是根提交:它有一个父d0ece So you've claimed that f8114a is both a root commit (no parents) and a regular everyday commit (one parent, d0ece ), and this is simply not possible.因此,您声称f8114a既是根提交(没有父级)又是常规的日常提交(一个父级, d0ece ),这根本不可能。

You may be able to get what you want .你也许能得到你想要的。 This depends on precisely what it is that you do want.这完全取决于您想要什么。 Trying to do this with git rebase is usually a bad idea, though: if there are any branch-and-merge operations in the set of commits you're trying to graft onto one of your branches, rebase itself will have to re-perform any of the merges involved, and that's a recipe for breaking stuff.但是,尝试使用git rebase通常是一个坏主意:如果尝试移植到其中一个分支的提交集中有任何分支和合并操作,rebase 本身将不得不重新执行所涉及的任何合并,这是破坏东西的秘诀。

Now, remember that any commit, whatever its hash ID might be, and whatever parent hash IDs it might have, represents a full and complete snapshot of the files that are contained inside that commit.现在,请记住,任何提交,无论其 hash ID 可能是什么,以及它可能具有的父 hash ID 都代表了该提交中包含的文件的完整且完整的快照 So commit f8114a is a snapshot.所以提交f8114a一个快照。 Since it it has no parent (on the left side), Git will show it as a comparison against a pseudo-commit that has no files.由于它没有父级(在左侧),Git 将显示它作为与没有文件的伪提交的比较。 1 So f8114a consists of add all of its files , when viewed as a set of changes. 1因此,当被视为一组更改时, f8114a包括添加其所有文件

If you take f8114a and copy it to a new-and-¿improved?如果你把f8114a复制到一个新的和改进的? commit—let's call this f9114a , although the actual hash ID will probably be different, but we need to have something to call it 2 —that does have a parent commit, though, now git show and other Git operations that compare the copy against its parent will have a snapshot to compare against. commit—let's call this f9114a , although the actual hash ID will probably be different, but we need to have something to call it 2 —that does have a parent commit, though, now git show and other Git operations that compare the copy against its父级有一个快照进行比较。 So now, instead of add all of these files , the diff may say delete most of the files, and create these mostly new ones instead, then change a whole lot of a few remaining files .所以现在,不是添加所有这些文件,差异可能会说删除大部分文件,并创建这些大多是新的文件,然后更改大量剩余的文件

If that's OK with you, it's easy to get this to happen in your own repository, without having it happen in any clones anyone makes of your repository.如果这对您来说没问题,那么很容易在您自己的存储库中发生这种情况,而不会发生在任何人您的存储库进行的任何克隆中。 Just use git replace with the --graft option.只需使用git replace --graft选项。 See the git replace documentation for details.有关详细信息,请参阅git replace文档

Once you're satisfied with some replacement(s), you can use a no-op git filter-repo (or the old but still supported, and included with Git distributions, git filter-branch ) to rewrite commits reachable from the tip of imported-branch .一旦您对某些替换感到满意,您可以使用无操作git filter-repo (或旧但仍受支持,并包含在 Git 发行版中, git filter-branch的提示) imported-branch This will "cement the replacement in place" and allow you to discard the graft;这将“将替换物固定到位”并允许您丢弃移植物; cloning this filtered repository will produce a copy of the filtered repository, which no longer uses the replacement trick.克隆此过滤存储库将生成过滤存储库的副本,该副本不再使用替换技巧。


1 Technically Git uses the empty tree to fake up this pseudo-commit. 1从技术上讲,Git 使用空树来伪造这个伪提交。 It diffs your commit's tree against the empty tree to produce all the "add entire file" diff-hunks.它将您的提交树与空树进行比较,以生成所有“添加整个文件”差异块。

2 Call it Fred or Barney or Wilma if you like; 2如果你愿意,可以称它为 Fred、Barney 或 Wilma; just don't call it late for dinner.只是不要晚饭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM