如何将一个 Git 存储库变基到另一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2428137/
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-10 08:01:15  来源:igfitidea点击:

How to rebase one Git repository onto another one?

gitmergerepositoryrebase

提问by kroimon

I had one Git repository (A) which contains the development of a project until a certain point. Then I lost the USB stick this repo A was on. Luckily I had a backup of the latest commit, so I could create a new repository (B) later where I imported the latest project's state and continue development. Now I recovered that lost USB stick, so I have two Git repositories.

我有一个 Git 存储库 (A),其中包含某个项目的开发直到某个时间点。然后我丢失了这个 repo A 所在的 USB 记忆棒。幸运的是我有最新提交的备份,所以我可以稍后创建一个新的存储库 (B),在那里我导入最新项目的状态并继续开发。现在我恢复了丢失的 U 盘,所以我有两个 Git 存储库。

I think I just have to rebase repo B onto repo A somehow, but I have no idea how to do that, maybe using fetch/pull and rebase?

我想我只需要以某种方式将 repo B 重新绑定到 repo A 上,但我不知道该怎么做,也许使用 fetch/pull 和 rebase?

回答by Josh Lee

If A and B are not the same repo (you created B by using the latest working copy you had), you have to use a graftto pretend that they have common history.

如果 A 和 B 不是同一个 repo(您使用最新的工作副本创建了 B),您必须使用嫁接来假装它们有共同的历史。

Let's assume you've added A as a remote for B as per VonC's answer, and the repo looks like this1:

假设您已根据VonC 的回答将 A 添加为 B 的遥控器,并且存储库如下所示1

~/B$ git tnylog 
* 6506232 (HEAD, master) Latest work on B
* 799d6ae Imported backup from USB stick
~/B$ git tnylog A/master
* 33b5b16 (A/master) Head of A
* 6092517 Initial commit

Create a graft telling the root of B that its parent is the head of A:

创建一个嫁接,告诉 B 的根节点它的父节点是 A 的头部:

echo '799d6aeb41095a8469d0a12167de8b45db02459c 33b5b16dde3af6f5592c2ca6a1a51d2e97357060' \
 >> .git/info/grafts

Now the two histories above will appear as one when you request the history for B. Making the graft permanent is a simple git filter-branchwith no arguments. After the filter-branch, though, you aren't on any branch, so you should git branch -D master; git checkout -b master.

现在,当您请求 B 的历史时,上面的两个历史将显示为一个。使移植永久化是一个简单git filter-branch的没有争论的问题。但是,在过滤器分支之后,您不在任何分支上,因此您应该git branch -D master; git checkout -b master.



1git tnylog= git log --oneline --graph --decorate

1git tnylog=git log --oneline --graph --decorate

回答by VonC

If A and B are the same repo (the first SHA1 are common), you can:

如果 A 和 B 是同一个 repo(第一个 SHA1 是通用的),您可以:

  • declare A as a remote for B: git remote add A /path/to/A
  • git fetch Ato update all remote A brancheson the B repo
  • git checkout dev(on B, where you are developing)
  • git rebase A/devBranchto replay B (i.e. what you develop or re-develop from your backup) on top of A/devBranch(the development you lost). A bit like this SO question.
  • 将 A 声明为 B 的遥控器: git remote add A /path/to/A
  • git fetch A更新所有远程甲分支在B回购
  • git checkout dev(在 B 上,您正在开发的地方)
  • git rebase A/devBranchA/devBranch(您丢失的开发)之上重放 B(即您开发或从备份重新开发的内容)。有点像这个SO 问题

The last step allows you to sync your dev with the one you lost.
But actually, once you have fetch from A, you are done: B now contains the "all" history (the one you lost and your current work)

最后一步允许您将开发人员与丢失的开发人员同步。
但实际上,一旦你从 A 获取,你就完成了:B 现在包含“所有”历史(你丢失的历史和你当前的工作)

回答by crazyscot

First of all, start by making a working clone of repo A.

首先,首先制作 repo A 的工作克隆。

Then simply pull into it from B and merge. You might prefer to create a new branch, pull onto it, then merge the two branches. You might also need a forcing flag; I've done things like this in Mercurial (grafting two apparently-unrelated repositories together) and it needs "-f".

然后简单地从 B 拉入它并合并。您可能更喜欢创建一个新分支,拉到它上面,然后合并两个分支。您可能还需要一个强制标志;我在 Mercurial 中做过这样的事情(将两个明显不相关的存储库嫁接在一起),它需要“-f”。