且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-08 09:42:49

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

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 graft to pretend that they have common history.

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

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

~/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

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

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

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

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

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-branch with no arguments. After the filter-branch, though, you aren’t on any branch, so you should git branch -D master; git checkout -b master.

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