Skip to content

Instantly share code, notes, and snippets.

@martinbuberl
Last active October 11, 2023 09:56
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save martinbuberl/b58fd967f271f32f51f50aee62e7332c to your computer and use it in GitHub Desktop.
Save martinbuberl/b58fd967f271f32f51f50aee62e7332c to your computer and use it in GitHub Desktop.
Import existing Git repository into another

Import existing Git repository into another

Folder structure before (2 separate repositories):

XXX
 |- .git
 |- (project files)
YYY
 |- .git
 |- (project files)

Folder structure after:

YYY
 |- .git  <-- This now contains the change history from XXX
 |-  ZZZ  <-- This was originally XXX
      |- (project files)
 |-  (project files)

In YYY:

git remote add XXX_remote <path-or-url-to-XXX-repo>
git fetch XXX_remote
git merge -s ours --no-commit XXX_remote/master
git read-tree --prefix=ZZZ/ -u XXX_remote/master
git commit -m "Imported XXX as a subtree."
git remote rm XXX_remote
git push

http://stackoverflow.com/a/8396318/135441

@christillman
Copy link

christillman commented Jun 4, 2018

May need to add --allow-unrelated-histories in the git merge, if you get "fatal: refusing to merge unrelated histories" and you know that's what you want.

@PabloCastellano
Copy link

PabloCastellano commented Jul 4, 2018

Really useful! Thanks 🎉

@TwoDollarsEsq
Copy link

Exactly what I needed. Thank you!

@opcloete
Copy link

opcloete commented Aug 1, 2018

Git amazes me daily. Thanks for the notes!

@calarasiu
Copy link

Thanks for the notes. It saved me a lot of browsing

@MattFL
Copy link

MattFL commented Feb 19, 2021

Thank you this was very helpful! @christillman your tip as well!

@sshveta
Copy link

sshveta commented Apr 30, 2021

Very useful. Thanks !

@SmartByAccident
Copy link

Doesnt keep file histories

@hustc12
Copy link

hustc12 commented Jul 4, 2022

Thanks! It's very helpful!

@Deepti0512
Copy link

If I delete XXX now, will it cause any effect in YYY ?

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