del-boy
del-boy

Reputation: 3654

Combine multiple orphan branches into one branch

I had a directory that contains multiple repositories and it looks like this:

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

I wanted to combine them into single git repository and I did that by following these instructions.

After these instruction I have following structure:

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

And that is great. But now I have main branch (from repo1) and two orphan branches in history:

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

What I would like to get is something like this

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

or even better

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

Is something like this possible? And how?


More details for answer provided by VonC.

I start with this:

$ 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

Then I do:

$ 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.

After that:

$ 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

Upvotes: 9

Views: 5320

Answers (1)

VonC
VonC

Reputation: 1324537

You could rebase the two orphan branches on top of your current master (from 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

But, in your case (after edits in the question), there is... nothing to do:

Those two orphan branches were already merged into master.
That is why a rebase would only fast-forward them to master... since they are already merged into master.
You can safely ignore eab1269 and 42333f: if they aren't referenced by a tag or a branch, they will be garbage-collected.
For instance, try to clone your repo and see if those commits are still around (if yes, try a git gc --aggressive --prune=now on that clone)


Note: git 2.9 (June 2016) will allow you to merge orphan branches (commit d04aa7e only with the --allow-unrelated-histories option:

git merge --allow-unrelated-histories a b

Upvotes: 14

Related Questions