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.

Watch a course Git & GitHub - The Practical Guide

Steps to combining projects into single repository

Let’s combine projects into one repository step by step:

Initializing a directory

First, open the terminal and create a directory and navigate to it:

mkdir test
cd test

Setting up a new repository

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/

Creating and commiting a new file

Next, you should create and commit a new file like this:

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

Adding a new repository as a subtree

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 [email protected]:exmaple/Example.git

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

Merging the 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.

Copying the history into the folder

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

Committing changes

Commit the changes to keep the projects safe:

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

Synchronizing the subtree with updates

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.