Create a Monorepo from Multiple Existing Repositories

  • Create a main directory and initialize Git
  • Clone each repository into a separate folder
  • Move contents and rename tags
  • Merge repositories into the monorepo
  • Update README with links to each repository
bash
#!/bin/bash

# List of repository names (update with actual repository names)
repos=("repo-1" "repo-2" "repo-3" "repo-4" "repo-5" "repo-6" "repo-7" "repo-8" "repo-9" "repo-11" "repo-12" "repo-13" "repo-14" "repo-15" "repo-16" "repo-17" "repo-18" "repo-19" "repo-20" "repo-21" "repo-22" "repo-23" "repo-24" "repo-25" "repo-26" "repo-27")

# Step 1: Create the monorepo
mkdir monorepo
cd monorepo
git init

echo "# Monorepo" > README.md
echo -e "\n## Included Repositories\n" >> README.md
git add README.md
git commit -m "Initial commit with README.md structure"

cd ..
mkdir repos

# Step 2: Clone each repo, move files into its own folder, and rename tags
for reponame in "${repos[@]}"; do
    cd repos
    git clone https://github.com/yourgithubaccount/$reponame.git
    cd $reponame
    mkdir -p $reponame
    find . -maxdepth 1 ! -name .git ! -name . -exec mv {} $reponame/ \;
    git add .
    git commit -m "Moved repo content into folder $reponame"
    git tag -l | xargs -I {} git tag "$reponame-{}"
    cd ..
    cd ..
done

# Step 3: Add each repository as a remote, merge into the monorepo, and update README.md
cd monorepo
count=1
for reponame in "${repos[@]}"; do
    git remote add -f $reponame ../repos/$reponame
    git merge $reponame/master --allow-unrelated-histories --no-edit
    echo "$count. [$reponame](./$reponame)" >> README.md
    ((count++))
done

git add README.md
git commit -m "Updated README.md with links to merged repositories"