Skip to content

Instantly share code, notes, and snippets.

@martinbuberl
Last active August 28, 2024 14:46
Show Gist options
  • 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

@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