将Git存储库拆分为两个

时间:2015-01-08 21:26:35

标签: git shell github repository git-branch

我有一个包含多个分支的git存储库,我想将它拆分为两个存储库。

想象一下,我列出了对该存储库所做的所有提交,结果如下:

Commit #1 to branch master
Commit #2 to branch master
Commit #3 to branch fixing_bugs
Commit #4 to branch master
Commit #5 to branch fixing_bugs
Commit #6 to branch master
Commit #7 to branch adding_sexy_french_girls_to_the_code
Commit #8 to branch adding_sexy_french_girls_to_the_code
Commit #9 to branch master

总的来说,这个存储库有 3 分支: master fixing_bugs adding_sexy_french_girls_to_the_code

现在我想使用commit#6将此存储库拆分为两个。所以我会有两个存储库,如下所示:

存储库1

Commit #1 to branch master
Commit #2 to branch master
Commit #3 to branch fixing_bugs
Commit #4 to branch master
Commit #5 to branch fixing_bugs

拥有分支 master fixing_bugs

存储库2

Commit #6 to branch master
Commit #7 to branch adding_sexy_french_girls_to_the_code
Commit #8 to branch adding_sexy_french_girls_to_the_code
Commit #9 to branch master

拥有分支 adding_sexy_french_girls_to_the_code

我知道这可能在技术上具有挑战性,但我相信用Git( le hope )并非不可能。

我该怎么做?

谢谢。

2 个答案:

答案 0 :(得分:1)

使用

从主人创建第二个分支
git checkout -b <your_new_branch>

我建议你从每个分支中的相同代码开始,然后通过以下方式重写历史记录:

git rebase HEAD~9 -i

然后您可以轻松删除每个分支所需的行。

因为您正在重写您的历史记录,所以您必须推动更改:

git push origin <your_branch> --force

<强> [UPDATE] 我看到了最后的评论,然后你将不得不在存储库2上压缩你的提交,以便用你的最后一次提交#6替换每个picks壁球。

在存储库1上git rebase HEAD~9 -i之后:

pick #1 to branch master
pick #2 to branch master
pick #3 to branch fixing_bugs
pick #4 to branch master
pick #5 to branch fixing_bugs
pick #6 to branch master
pick #7 to branch adding_sexy_french_girls_to_the_code
pick #8 to branch adding_sexy_french_girls_to_the_code
pick #9 to branch master 

你应该:

pick #1 to branch master
pick #2 to branch master
pick #3 to branch fixing_bugs
pick #4 to branch master
pick #5 to branch fixing_bugs

(在vim上键入dd以删除行)

在存储库2上git rebase HEAD~9 -i之后:

pick #1 to branch master
pick #2 to branch master
pick #3 to branch fixing_bugs
pick #4 to branch master
pick #5 to branch fixing_bugs
pick #6 to branch master
pick #7 to branch adding_sexy_french_girls_to_the_code
pick #8 to branch adding_sexy_french_girls_to_the_code
pick #9 to branch master 

你应该:

s #1 to branch master
s #2 to branch master
s #3 to branch fixing_bugs
s #4 to branch master
s #5 to branch fixing_bugs
pick #6 to branch master
pick #7 to branch adding_sexy_french_girls_to_the_code
pick #8 to branch adding_sexy_french_girls_to_the_code
pick #9 to branch master 

(编辑线条,前5行将用#6压扁)

答案 1 :(得分:1)

您可以使用git format-patchgit am将提交从现有存储库复制到新存储库,然后使用git reset从现有存储库中删除复制的提交。< / p>

尝试使用这些命令将提交复制到新存储库:

$ cd /path/to/new_repo_folder # don't forget to run git init if this is a new folder
$ git --git-dir=/path/to/existing_repo_folder/.git format-patch --stdout sha1_of_commit5..sha1_of_commit_9 | git am

基本上,git format-patch命令为每个提交创建一个补丁文件,从提交6到提交9.如果未提供--stdout选项,这些补丁文件将保存在您的existing_repo_folder。随后,git am命令将提交6的历史记录应用于new_repo_folder中的提交9。现在,当您在git log中运行new_repo_folder时,您将看到提交6到9的历史记录。

最后,如果提交9是您的HEAD,那么您可以在git reset --hard HEAD~3中运行existing_repo_folder以将HEAD的位置设置为提交5,这是3次提交之前,这就是~3代表的内容。

当然,您实际上是您的存储库的rewriting the history。如果您与其他人分享您的代码,请确保您达成共识。否则,你的下一个git push -f会让每个人都感到悲伤。