Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active March 31, 2019 18:15
Show Gist options
  • Save plembo/07bbf3aee8512c6a2bcda14f454a31a0 to your computer and use it in GitHub Desktop.
Save plembo/07bbf3aee8512c6a2bcda14f454a31a0 to your computer and use it in GitHub Desktop.
Combine git repositories

Combine multiple git repositories

Wanted to consolidate multiple git repos into one, and preserve history from each.

  1. Create folder to hold repos.
$ cd ~/
$ mkdir src
  1. Create new repo.
$ cd src
$ mkdir datacamp
$ cd datacamp
$ git init
  1. Clone repos to be combined.
$ cd ~/src
$ git clone [email protected]:plembo/intro-python.git
$ git clone [email protected]:plembo/inter-python.git
$ git clone [email protected]:plembo/intro-sql.git
  1. Prep old repo for merge.
$ cd intro-python
$ git remote rm origin
$ mkdir intro-python
$ git mv *.* intro-python/
$ git commit -m "Prep repo for merge"
$ cd ../datacamp

Removing the old remote origin is a safety measure. A generic git mv "." may not move all files and folders (like a .gitignore, for example). Be prepared to manually capture the rest with additional git mv commands.

  1. Merge old repo into new repo.
$ cd ~/src/datacamp
$ git remote add intro-python ~/src/intro-python
$ git pull intro-python master --allow-unrelated-histories
$ git remote rm intro-python-master
$ cd ..
$ rm -rf intro-python-master
  1. Repeat steps 4 and 5 for each remaining repo.

  2. Create new repo in cloud (I always do this is the Github gui, but feel free to use the API).

  3. Push new repo content and history to the cloud.

$ cd ~/src/datacamp
$ git remote add origin [email protected]:plembo/datacamp.git
$ git push -u origin master

NOTE: If you find yourself needing to add additional repos to your consolidated repo once it has been pushed up to the cloud, you can simply process the repo you want to add as describe in step 4 and then follow step 5 to merge. The only difference will be in the file paths. A simple git push from the cloud connected repo should suffice to upload the newly merged changes.

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