I was working on a project, made some progress, commited a bunch of code into github and all was well. The code eventually needed to go into another repo so the painful merge adventure began… man… That took a bit of research.
I did what I do best and googled the hell out of it… There were lots of solutions but some of them weren’t really clear to me or just didn’t work. So after mindlessly trying out solutions, I realized I had to actually think of one.
Take the contents of a repo and put it inside a particular folder in an existing repository and at the same time preserve the history.
In an effort to save myself some future research… And maybe someone else needs this info.
If you have a better solution, let me know!
Set up a separate area and clone both the original repository Orig
– which contains the files you want to copy – and the target repository Dest
. This way, you can delete and restart if something goes wrong. Don’t push changes unless you’re really sure.
$ mkdir TestFolder
$ cd TestFolder
$ git clone Orig.git
$ git clone Dest.git
|-- TestFolder
|-- Orig
|-- .git
|-- .gitignore
|-- File
|-- Dir
|-- Dest
|-- .git
|-- File
|-- File
Create a new folder – in this example, I named it temp
– inside the repo Orig
and move everything except the .git
folder into it. Make sure that the folder name you choose doesn’t exist in the root of the repo Dest
to avoid issues and conflicts.
$ cd /TestFolder/Orig
$ mkdir temp
$ mv !(temp) temp
$ mv .gitignore temp # Manually move files that are remaining
|-- TestFolder
|-- Orig
|-- .git
|-- temp
|-- .gitignore
|-- File
|-- Dir
|-- Dest
|-- .git
|-- File
|-- File
Commit the code but DO NOT PUSH!!! You don’t want to modify the origin.
$ git add -A . # The next line doesn't commit if this line is not there
$ git commit -a -m "Moved all data into temp folder"
Go to Dest
and add temp
as a remote branch.
$ cd /TestFolder/Dest
$ git remote add temp /TestFolder/Orig
$ git fetch temp
Merge the remote branch into Dest
.
$ git merge -m "Merging into repo Dest" temp/master
Remove the temp
remote branch from Repo Dest
.
$ git remote rm temp
You should now have the folder temp
in the root of Repo Target
|-- TestFolder
|-- Orig
|-- .git
|-- temp
|-- .gitignore
|-- File
|-- Dir
|-- Dest
|-- .git
|-- File
|-- File
|-- temp # This is the new folder
|-- .gitignore
|-- File
|-- Dir
And… if you look at the git logs, you’ll see that the history is there. Yay!
Now, you can move the contents of temp
anywhere in Dest
. You’re done! Commit and push!