How to Import Multiple Projects into a Single Git Repository

One of the ways of managing several projects within a single repository can be accomplished with Git Subtree merge. Typically, a subtree merge strategy is used to contain a repository within a repository. Let’s combine projects into one repository step by step.

First, let's set up an empty repository for a subtree merge. Open the terminal and create a directory and navigate to it:

mkdir test
cd test

Then, generate a new Git repository with git init. With this command, a .git subdirectory is created, which includes the metadata, like subdirectories for objects and template files for initializing a new repository:

git init
Initialized empty Git repository in /Users/octocat/tmp/test/.git/

Next, create and commit a new file:

touch .gitignore
git add .gitignore
git commit -m "initial commit"

Now, it’s time to add a new repository as a subtree. Add a new remote URL pointing to the separate project we want with the git remote command:

git remote add -f example git@github.com:exmaple/Example.git

The example is the subtree that we merge in the test repository.

Now, we can git merge the Example project into the local Git project, which won't change any of the files locally.

For Git 2.9 version or above run the following:

git merge -s ours --no-commit --allow-unrelated-histories example/master
Automatic merge went well; stopped before committing as requested.
By default, the git merge command refuses to merge histories that do not share a common ancestor. The --allow-unrelated-histories option is used to override this safety when merging histories of two projects.

For Git 2.8 version or below run:

git merge -s ours --no-commit example/master
Automatic merge went well; stopped before committing as requested.

Create a new folder called example, and copy the Git history of the Example project into it:

git read-tree --prefix=example/ -u example/master

Commit the changes to keep the projects safe:

git commit -m "Subtree merged in example"
[master ge1cf26] Subtree merged in example

The last step is synchronizing the subtree with updates and changes because it won't be kept in sync with the upstream changes. Execute the git pull command with -s option:

git pull -s subtree example master

Subtree Merge Strategy

Git Subtree allows nesting one repository inside another one as a subdirectory. It is one of the ways to track the history of software dependencies. But subtrees should not be confused with submodules. A subtree is just a subdirectory that you can commit to, branch, and merge along with your project.

Subtree merging strategy is used while performing merges with Git. It is useful when merging a branch into a subdirectory of a Git repository. The subtree merge strategy combines the history of the subproject with the history of the project, while the subproject's history can be kept clean except for the commits that should go upstream.


Do you find this helpful?

Related articles