开发者

Split two or more git branches into separate subdirectories of one repo?

开发者 https://www.devze.com 2023-01-05 22:49 出处:网络
This question is somewhat similar to How to combine two branches from two different repositories in a single repository?.

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消