Howto: Split a git repo and still retain history
At work, we have a project that started out small. As with most of our projects, it started with us learning more interesting and better ways of doing things as we became more familiar with the blocks with which we were building. Recently, the project grew big enough that it had actually become two projects. Unfortunately, they shared the same code repository, since they started as one. After a little bit of time in the office Om Lounge with my abacus, I figured out a fairly simple way to split these two repositories, but still retain the whole development history up until that point.
- Create KEEP and MOVE branches.
- Make the appropriate changes in each branch.
- Push the MOVE branch into master in the new repository.
- Merge the KEEP branch into master.
- Delete the MOVE branch.
- Repeat for your branches other than master.
(My company uses GitHub, so my examples are using that.)
If you don't have a repo you'd like to split, but would like to follow along, make your self some dummy repos on GitHub and then create some test files. In my example, I'm splitting up repoA. I'm going to keep file1 in repoA and move file2 into a brand new repo, repoB.
First you need to figure out everything we want to keep in repoA. Create a new branch for the files you're keeping. Use
git rm and
git mv to rearrange the directory, reflecting it's look after the move is complete. Don't worry about deleting files, they all still exist in previous commits. (You did commit everything, right?)
Next, you need to figure out everything that's getting move to repoB. Check out master, so you get your original state back, then create a new branch for the files you're moving. Again, use
git rm and
git mv to rearrange the directory, reflecting the new repo's look after move is complete.
Now we're going to push the files that are moving, into their new home. Don't worry about setting up remotes, since you'll (hopefully) only be using this remote once. (Nerd note: remotes are just shorthand notation for a git URL, so we can use the URL directly here. Read more here)
Now that the moving files are safely moved to their destination, we need to finalize the move by merging the branch of files that are sticking around.
All that's left now, is to push our changes back to the original repository and clean up a bit.
You'll notice that git complains when it tries to delete the branch of files that are moving. It's letting us know that we're removing information from our repo by deleting that branch. We'll have to use -D instead of -d to let it know we really mean it.
That's it! Pretty easy, huh?
Also, check out all the gist stuff on one page here: Gist.
blog comments powered by Disqus