Merge a Git repository and its history into a subdirectory of a second Git repository

On more than one occasion, I have needed to merge a Git repository and its history into a subdirectory of a second Git repository.

In this post, I show how to merge a Git repo git@git.example.com:PUP/puppet-roles.git (hereafter “puppet-roles”) into the modules/ directory a second Git repo git@git.example.com:PUP/control-repo.git (“control-repo”). And after the merge, I show how to filter the history so that commands like git log, git blame and git show all work as expected, and show a history as if the files in the subdirectory had always been there.

Set up a test environment

We begin by cloning control-repo into /var/tmp as follows:

$ cd /var/tmp $ git clone git@git.example.com:PUP/control-repo.git read more