2013-10-06 33 views
4

我有一个包含多个存储库的目录,它看起来像这样:将多个孤儿分支成一路

folder 
|- repo1 
| |- .git 
| |- File1.txt 
|- repo2 
| |- .git 
| |- File2.txt 
|- repo3 
| |- .git 
| |- File3 

我想将它们合并为单一的Git仓库而我做了由 以下these说明。

这些指令后,我有以下结构:

folder 
|- .git 
|- repo1 
| |- File1.txt 
|- repo2 
| |- File2.txt 
|- repo3 
| |- File3 

这是伟大的。 但现在我有主枝(从repo1)和从两部孤儿分支:

* Merge repo3 
|\ 
| * Add third file 
* Merge repo2 
|\ 
| * Add second file 
* Add first file 

我希望得到的是这样的

* Merge repo 3 
* Add third file 
* Merge repo 2 
* Add second file 
* Add first file 

甚至更​​好

* Add third file 
* Add second file 
* Add first file. 

是这样的可能吗?如何?


更多答案由VonC提供。

我开始与此:

$ git log --graph 
* commit 888bb0d7a81a40f8b42da7ad3f02103e898f5c1f 
|\ Merge: cf85078 eab1269 
| | Author: Bojan Delic <...> 
| | Date: Sun Oct 6 13:12:36 2013 +0200 
| | 
| |  Merge repo3 
| | 
| * commit eab1269be53929450c34c30416820c13a8058678 
| Author: Bojan Delic <...> 
| Date: Sun Oct 6 13:07:39 2013 +0200 
| 
|  Add third file 
| 
* commit cf85078ca96d52f2277ad7e4c6f45b04a38cf7ee 
|\ Merge: 92fa311 42333f8 
| | Author: Bojan Delic <...> 
| | Date: Sun Oct 6 13:12:33 2013 +0200 
| | 
| |  Merge repo2 
| | 
| * commit 42333f8589e20d29e5e444d5e16ff1a5d63b8288 
| Author: Bojan Delic <...> 
| Date: Sun Oct 6 13:07:09 2013 +0200 
| 
|  Add second file 
| 
* commit 92fa3113507548a62407431188c308685f72865d 
    Author: Bojan Delic <...> 
    Date: Sun Oct 6 13:06:39 2013 +0200 

     Add first file 

然后我做的:

$ git branch -v 
* master 888bb0d Merge repo3 
$ git checkout -b branch2 eab1269be53929450c34c30416820c13a8058678 
$ git checkout -b branch3 42333f8589e20d29e5e444d5e16ff1a5d63b8288 
$ git checkout branch2 
Switched to branch 'branch2' 
$ git rebase master 
First, rewinding head to replay your work on top of it... 
Fast-forwarded branch2 to master. 
$ git checkout master 
Switched to branch 'master' 
$ git merge branch2 
Already up-to-date. 
$ git checkout branch3 
Switched to branch 'branch3' 
$ git rebase master 
First, rewinding head to replay your work on top of it... 
Fast-forwarded branch3 to master. 
$ git checkout master 
Switched to branch 'master' 
$ git merge branch3 
Already up-to-date. 

之后:

$ git log --graph 
* commit 888bb0d7a81a40f8b42da7ad3f02103e898f5c1f 
|\ Merge: cf85078 eab1269 
| | Author: Bojan Delic <...> 
| | Date: Sun Oct 6 13:12:36 2013 +0200 
| | 
| |  Merge repo3 
| | 
| * commit eab1269be53929450c34c30416820c13a8058678 
| Author: Bojan Delic <...> 
| Date: Sun Oct 6 13:07:39 2013 +0200 
| 
|  Add third file 
| 
* commit cf85078ca96d52f2277ad7e4c6f45b04a38cf7ee 
|\ Merge: 92fa311 42333f8 
| | Author: Bojan Delic <...> 
| | Date: Sun Oct 6 13:12:33 2013 +0200 
| | 
| |  Merge repo2 
| | 
| * commit 42333f8589e20d29e5e444d5e16ff1a5d63b8288 
| Author: Bojan Delic <...> 
| Date: Sun Oct 6 13:07:09 2013 +0200 
| 
|  Add second file 
| 
* commit 92fa3113507548a62407431188c308685f72865d 
    Author: Bojan Delic <...> 
    Date: Sun Oct 6 13:06:39 2013 +0200 

     Add first file 

$ git branch -a 
    branch2 
    branch3 
* master 

回答

6

你可以变基您当前master的顶部的两个孤儿分支(来自repo1

git checkout branch2 
git rebase master 
git checkout master 
git merge branch2 # fast-forward master to branch2 

git checkout branch3 
git rebase master 
git checkout master 
git merge branch3 # fast-forward master to branch3 

但是,你的情况(在后的问题编辑),有...无关:

这两个孤儿分公司分别已经合并为master
这就是为什么rebase只会将它们快速转发到master ......因为它们已经合并为master
你可以放心地忽略eab126942333f:如果它们没有被标签或分支引用,它们将被垃圾收集。
例如,尝试克隆你的回购,看看这些提交仍然在周围(如果是,尝试git gc --aggressive --prune=now于克隆)


注:混帐2。9(2016年6月)将让你合并孤儿分支commit d04aa7e仅与--allow-unrelated-histories选项:

git merge --allow-unrelated-histories a b 
+0

也许我不明白,但没有BRANCH2,只有'master'的和'HEAD ',并且他们指向提交'合并repo3',如果我添加了分支,指向提交'添加第三个文件'并且进行rebase和merge,就像你所建议的那样,新分支移动到'合并repo3' commit,但是孤立分支仍然存在。 –

+0

@ del-boy你说有两个孤儿分支,所以我把他们称为'branch2'和'branch3'。'git branch -v'返回什么? – VonC

+0

是的,我很伤心有两个孤儿分支,因为我发现了这个术语,但是没有任何指向anywh的真正的git分支除了主人之外,那指向头。 'git branch -v'只返回'* master 888bb0d合并repo3'。 –