Blog Tags: 

Git tip: how to merge multiple projects into one big repository

A while back I decided it would be a good idea to combine a loose collection of related Git repositories into one big Git repository.

The rational for this was that I noticed that often the same logical change had to be broken up into multiple commits across these previously separate repositories in a way that made it difficult to track which changes were part of the same change. In other words it artificially fragmented the commits and made revisions harder to track.

I wanted future changes to go into one big Git repository but I also wanted to preserve the history of the component repositories.

Once I figure out how to do it, this was very easy to implement. I simply moved the contents of each repository so that everything was under a single subdirectory, like this:

cd foo/
mkdir foo
git-mv * foo
git-add .
git-commit -v -m "moved everything under foo/"

Once I did that for all the repositories (e.g., foo, bar), I created a new repository and merged all of these components repositories into that single repo:

mkdir merged-project
    cd merged-project
    git-pull ../foo
    git-pull ../bar

When all of this is done, you get one big repository has the component projects not as separate repositories but simple sub-directories in the big repository, with all of the commit history perfectly preserved.


Jeremy Davis's picture

Just looking at it it appears that in the mv line, the asterisk would indeed capture the intended target directory too (and produce the error you note).

I reckon when he did it for real, he explicitly moved the directories/files. Then overlooked that when he generalised the procedure as example.


Add new comment