Wanted to consolidate multiple git repos into one, and preserve history from each.
- Create folder to hold repos.
$ cd ~/
$ mkdir src
- Create new repo.
$ cd src
$ mkdir datacamp
$ cd datacamp
$ git init
- 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
- 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.
- 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
-
Repeat steps 4 and 5 for each remaining repo.
-
Create new repo in cloud (I always do this is the Github gui, but feel free to use the API).
-
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.