将Git库分成两部分 [英] Split a Git Repository Into Two
问题描述
想象一下,我列出了对该存储库所做的所有提交,并且结果如下所示:
将#1提交给分支主设备
将#2提交给分支主设备
提交#3到分支fixed_bugs
提交#4到分支master
提交#5到分支fixing_bugs
提交#6到分支master
提交#7到分支adding_sexy_french_girls_to_the_code
提交#8到分支加入_sexy_french_girls_to_the_code
提交#9给分支主
存储库具有 3 分支: master , fixing_bugs 和 adding_sexy_french_girls_to_the_code 。
现在我想使用提交#6将这个存储库分成两部分。因此,我将两个存储库如下所示:
存储库1
提交#1到分支master
提交#2到分支master
提交#3到分支fixing_bugs
提交#4到分支master
将#5提交给branch fixed_bugs
拥有分支 master , fixing_bugs 。
存储库2 拥有分行 master , adding_sexy_french_girls_to_the_code 。 我知道这在技术上可能是有挑战性的,但我相信用Git来做并不是不可能的( le hope )。 我该如何做到这一点? 谢谢。
<$ p $将#6提交给分支master
提交#7以分支adding_sexy_french_girls_to_the_code
提交#8以分支adding_sexy_french_girls_to_the_code
将#9提交给分支master
你可以使用 git format-patch
和 git am
将提交从现有存储库复制到新存储库,然后按< a href =http://git-scm.com/docs/git-reset =nofollow> git reset
删除复制的提交
尝试使用这些命令将提交复制到新的存储库:
$ cd / path / to / new_repo_folder#如果这是一个新文件夹,请不要忘记运行git init $ b $ 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
命令在您的 new_repo_folder
中应用提交6的提交历史记录。现在当您在 new_repo_folder
中运行 git log
时,您会看到提交6到9的历史记录。
最后,如果提交9是你的 HEAD
,那么你可以运行 git reset --hard HEAD在
existing_repo_folder
中设置 HEAD
的位置为commit 5,这是3提交之前,这就是〜3
代表的内容。
当然,你基本上是重写版本库的历史。如果你与其他人分享你的代码,请确保你有他们的共识。否则,你的下一个 git push -f
会给每个人带来很大的悲伤。
I have a git repository with several branches, and I would like to split it in two repositories.
Imagine that I list all commits made to that repository, and the result is something like:
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
So in total, this repository has 3 branches: master, fixing_bugs and adding_sexy_french_girls_to_the_code.
Now I want to split this repository in two, using commit #6. So I would have two repositories like the following:
Repository 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
Having branches master, fixing_bugs.
Repository 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
Having branches master, adding_sexy_french_girls_to_the_code.
I understand this may be technically challenging, but I believe it is not impossible to do with Git (le hope).
How can I do this?
Thank you.
You can make use of git format-patch
and git am
to copy commits from your existing repository to the new one, follow by a git reset
to remove the copied commits from the existing repository.
Try these commands to copy the commits to the new repository:
$ 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
So essentially, the git format-patch
command creates one patch file for each of your commit, from commit 6 to commit 9. If the --stdout
option is not provided, these patch files will be saved in your existing_repo_folder
. Subsequently, the git am
command applies the history of commit 6 to commit 9 in your new_repo_folder
. Now when you run git log
in your new_repo_folder
, you will see the history of commit 6 to 9.
Finally, if commit 9 is your HEAD
, then you can run git reset --hard HEAD~3
in your existing_repo_folder
to set the position of HEAD
to commit 5, which is 3 commits before, and that's what that ~3
represents.
Of course, you are essentially rewriting the history of your repository. If you are sharing your codes with other people, make sure you have their consensus. Otherwise, your next git push -f
is going to cause everyone a lot of grief.
这篇关于将Git库分成两部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!