W3docs

How to Import Multiple Projects into a Single Git Repository

In this tutorial, you will get an answer to your problem of managing multiple projects into a single repository step by step with the most used commands.

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.

Steps to combining projects into single repository

Let’s combine projects into one repository step by step. The following steps demonstrate importing a single project; repeat them for each additional project:

Initializing a directory

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

create a directory git

mkdir test
cd test

Setting up a new repository

Then, generate a new Git repository with git init. With this command, a <kbd class="highlighted">.git</kbd> subdirectory is created, which includes the metadata, like subdirectories for objects and template files for initializing a new repository:

generate a new git repository

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

Creating and committing a new file

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

create and commit a new file

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:

add a new repository as a subtree git

git remote add -f example [email protected]:example/Example.git

The remote name example refers to the upstream repository Example.

Merging the repository

Now, we can git merge the <kbd class="highlighted">Example</kbd> 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

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

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

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 <kbd class="highlighted">Example</kbd>, and copy the Git history of the Example project into it:

copy the git history

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

Committing changes

Commit the changes to keep the projects safe:

commit the changes

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 <kbd class="highlighted">-s</kbd> option:

synchronizing the subtree with updates and changes git

git pull -s subtree example master

To push local changes back to the upstream repository, run:

git subtree push -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.