Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save etiennecollin/bc84a5d4ad4f533315a7a628dd44e48c to your computer and use it in GitHub Desktop.

Select an option

Save etiennecollin/bc84a5d4ad4f533315a7a628dd44e48c to your computer and use it in GitHub Desktop.
Merge multiple repositories into one single repository

Merge multiple repositories into one single repository

If you have created multiple repositories and now find it cluttered, you can merge them into a single repository while preserving all the commit history. Here’s how to do it.

1. Create a New Repository:

Create a new repository on GitHub or locally on your machine.

git init new-repo
cd new-repo

2. Add the Other Repositories as Remotes:

Add the repositories you want to merge as remotes in the new repository.

git remote add repo1 <url-of-repo1>
git remote add repo2 <url-of-repo2>

3. Fetch the Commits from the Other Repositories:

Fetch the commits from the other repositories.

git fetch repo1
git fetch repo2

4. Create New Branches for Each Repository:

Create new branches for each repository to bring in the files.

git checkout -b branch-repo1 repo1/main
git checkout -b branch-repo2 repo2/main

5. Move Files to Subdirectories:

Move the files from each branch to a subdirectory to avoid conflicts.

git checkout branch-repo1
mkdir repo1
git mv * repo1/
git commit -m "Moved repo1 files to subdirectory"

git checkout branch-repo2
mkdir repo2
git mv * repo2/
git commit -m "Moved repo2 files to subdirectory"

6. Merge the Branches into the Main Branch:

Merge the branches into the main branch and resolve any conflicts that may arise.

git checkout main
git merge branch-repo1
git merge branch-repo2

7. Push the Changes to the Remote Repository:

If you are using a remote repository, push the changes to it.

git remote add origin <url-of-new-repo>
git push -u origin main

Source

Source: https://github.com/orgs/community/discussions/143759#discussioncomment-11167011

The code snippets were slightly modified.

Thank you to @abdulrahmanRadan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment