tags:

views:

52

answers:

1

Hey folks,

This question is somewhat similar to How to combine two branches from two different repositories in a single repository?.

However, I want to combine two branches from the same repository into one branch, but in separate subdirectories. So, starting with one repo that has two branches:

/appFoo
  -MasterBranch
  -OtherVersionBranch

And ending up with a repo with one branch(master) and two subdirectories:

/appFoo
    /MasterSubdirectory
    /OtherVersionSubdirectory

And of course I'd like to keep the histories intact, so if I view the log of a file in OtherVersionSubdirectory I see all the commits that had been made to that branch.

Basically what started as a development branch evolved into a custom version for another customer, and so we don't feel that treating it as a branch of master makes sense any more.

Thanks everyone!

+2  A: 

Switch to branch MasterBranch and throw the contents of the branch into directory MasterSubdirectory, commit:

git checkout MasterBranch
mkdir MasterSubdirectory
git mv -k * MasterSubdirectory
git commit -a

This leaves your branch with just one dir.

Do analogically in OtherVersionBranch:

git checkout OtherVersionBranch
mkdir OtherVersionSubdirectory
git mv -k * OtherVersionSubdirectory
git commit -a

Merge one branch into another

git checkout MasterBranch
git merge OtherVersionBranch
git branch -d OtherVersionBranch

Now you have a single branch MasterBranch. You can merge it with master or do whatever you want with it.

Alternatively, you may want to replace merge with rebase, if you don't mind history rewriting. This will produce a cleaner order of commits - first one branch, later the other.

And do try this on a copy. It worked for me, but my case was very simple.

Michał Trybus
I'm getting a great deal of "CONFLICT (rename/rename)" conflicts when I do the merge. It seems to be on all the files that are the same between the branches. Seems like git wants to do a rename on those files instead of a merge. What's the "yes, I really want this" switch in this situation?
Mojo Hand
It's strange, but maybe there's something in your repo I didn't take into account. I tried a scenario where I had 2 files in master, then created new branch, modified both, then on master modified both but differently, so the branches completely diverged, and then I did as I wrote in the answer and it just worked.Are you sure both branches had just single directories of different names just before you merged?
Michał Trybus
Well, the directories are not quite as simple as that, but git is complaining about that the files that are the same, not the ones that differ.
Mojo Hand
It seems you'll have to wait for someone to post a better solution or solve the conflicts:/
Michał Trybus
Ok, think I have it working. Before it would let me commit after that last merge I had to do "git rm" on all the files that used to be there, even though they weren't actually there anymore. Now if I want to get the full history for a file of either version I have to pass "--find-copies-harder" to "git log", but I think this is as good as I can expect. Thanks so much for all the time you spent with this.
Mojo Hand
It's good to hear it works at least. A cleaner solution anyone?
Michał Trybus