header image

Code Snippets

Merge Git Repository as Subdirectory

| Last updated:
git remote add project-b <project-b git remote>
git fetch project-b
git branch branch-from-project-b project-b/main
git checkout branch-from-project-b
mkdir project-b
git ls-tree -z --name-only HEAD | xargs -0 -I {} git mv {} project-b/
git commit -m "Merge project-b repository as subdirectory project-b"
git checkout main
git pull
git checkout -b merge-project-b-as-subdirectory
git merge --allow-unrelated-histories branch-from-project-b
git push

Create git branch in project-a with project-b added in directory ./project-b.

Git history is retained.