Git isn't exactly good at tracking files getting moved around, so if you want to preserve file history, you're going to have to make it so the old repository always had its files in the sub-directory you now need it to be in.
git-filter-branch
works decently enough, but is no longer recommended. The man page now points to git-filter-repo
, which lets you retroactively move files around (among other things you can do with git-filter-branch
) in a much simpler way.
Copied from my own answer here:
export SUBTREE_PREFIX="subproj1"
git remote add -f "${SUBTREE_PREFIX:?}-remote" https://my-git-repo.invalid/subproj1.git
git checkout "${SUBTREE_PREFIX:?}-remote"/master -b "${SUBTREE_PREFIX:?}-master"
# --force is to skip the "freshly cloned repo" check.
# All the refs we'll be operating on are fresh, even if the repo isn't
# Remove --dry-run once you've checked .git/filter-repo/fast-export.filtered
# to be sure that everything is correct.
git filter-repo --refs "${SUBTREE_PREFIX:?}-master" --to-subdirectory-filter "${SUBTREE_PREFIX:?}" --force --dry-run
git checkout master
git merge "${SUBTREE_PREFIX:?}-master" --allow-unrelated-histories
# Repeat for however many repos you need to add
See also How do you merge two Git repositories?