Merge existing repository into a subdirectory/subfolder

How to merge an existing repository into a new subdirectory of the current repository?

Here is the answer!

If the exist repo path is: /path/to/ex/repo, and you can give it a new name such as: ExRepo

If you want to import this repo to the current repository as sub-directory SubDir:

  1. git remote add -f ExRepo /path/to/ex/repo
  2. git merge -s ours –no-commit ExRepo/master
  3. git read-tree –prefix=SubDir/ -u ExRepo/master
  4. git commit -m “Merge ExRepo as our subdirectory SubDir

It’s done! Just four lines. What do these steps mean?

  1. fetch the other project “ExRepo
  2. prepare for the later step to record the result as a merge
  3. read “master” branch of ExRepo to the subdirectory “SubDir
  4. record the merge result

You may commit exist changes first if you have got the error “Merge with strategy ours failed“.

One more thing, how to update SubDir after merging is simple the same. Just type:

  1. git pull -s subtree ExRepo master

Enjoy & Good lock!

 

 

Published by

vivo

This is Vivo. Yes, I'm Vivo.

Leave a Reply

Your email address will not be published. Required fields are marked *