Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple orphan branches into one branch

Tags:

git

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
like image 918
del-boy Avatar asked Oct 06 '13 11:10

del-boy


People also ask

How do I combine multiple branches in one branch?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

How do you combine branches?

First we run git checkout master to change the active branch back to the master branch. Then we run the command git merge new-branch to merge the new feature into the master branch. Note: git merge merges the specified branch into the currently active branch.

Can you merge a branch multiple times?

Merging a branch multiple times into another works fine if there were changes to merge. Save this answer. Show activity on this post. Actually yes you totally can, when you merge again it will bring over any commits that don't exist on production.

How do you make an orphan branch?

Create an Orphan Branch An orphan branch is a separate branch that starts with a different root commit. So the first commit in this branch will be the root of this branch without having any history. It can be accomplished by using the Git checkout command with the ––orphan option.


1 Answers

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
like image 92
VonC Avatar answered Sep 24 '22 17:09

VonC