Skip to content

Instantly share code, notes, and snippets.

@martinbuberl
Last active May 28, 2025 21:05
Show Gist options
  • Select an option

  • Save martinbuberl/b58fd967f271f32f51f50aee62e7332c to your computer and use it in GitHub Desktop.

Select an option

Save martinbuberl/b58fd967f271f32f51f50aee62e7332c to your computer and use it in GitHub Desktop.

Revisions

  1. martinbuberl revised this gist Mar 27, 2025. 1 changed file with 24 additions and 17 deletions.
    41 changes: 24 additions & 17 deletions git-import-repository.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Import existing Git repository into another
    # Import an Existing Git Repository into Another

    Folder structure before (2 separate repositories):
    ## Before: Folder Structure (Two Separate Repositories)

    ```
    XXX
    @@ -11,26 +11,33 @@ YYY
    |- (project files)
    ```

    Folder structure after:
    ## After: Folder Structure (XXX Imported into YYY as a Subdirectory)

    ```
    YYY
    |- .git <-- This now contains the change history from XXX
    |- ZZZ <-- This was originally XXX
    |- (project files)
    |- (project files)
    |- .git <-- This now contains the full change history from XXX
    |- ZZZ <-- This was originally XXX, now a subdirectory of YYY
    |- (project files)
    |- (project files from YYY)
    ```

    In YYY:
    ## Steps to Import

    Run the following commands inside **YYY** to import **XXX** as a subtree:

    ```sh
    git remote add XXX_remote <path-or-url-to-XXX-repo> # Add XXX as a remote
    git fetch XXX_remote # Fetch history from XXX
    git merge -s ours --no-commit XXX_remote/master # Merge without modifying YYY's files
    git read-tree --prefix=ZZZ/ -u XXX_remote/master # Move XXX into subdirectory ZZZ
    git commit -m "Imported XXX as a subtree." # Commit the changes
    git remote rm XXX_remote # Remove temporary remote
    git push # Push changes to the repository
    ```
    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
    ### Explanation
    - **Preserves full commit history** of XXX.
    - **Places XXX’s files inside `ZZZ/`** within YYY.
    - **Does not modify YYY’s existing files** during the merge.

    [Source: Stack Overflow](http://stackoverflow.com/a/8396318/135441)
  2. martinbuberl revised this gist Oct 25, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions git-import-repository.md
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,8 @@ 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
  3. martinbuberl revised this gist Oct 25, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-import-repository.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ YYY

    In YYY:

    ```sh
    ```
    git remote add XXX_remote <path-or-url-to-XXX-repo>
    git fetch XXX_remote
    git merge -s ours --no-commit XXX_remote/master
  4. martinbuberl revised this gist Oct 25, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-import-repository.md
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ YYY

    In YYY:

    ```
    ```sh
    git remote add XXX_remote <path-or-url-to-XXX-repo>
    git fetch XXX_remote
    git merge -s ours --no-commit XXX_remote/master
  5. martinbuberl renamed this gist Oct 25, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. martinbuberl renamed this gist Oct 25, 2016. 1 changed file with 0 additions and 0 deletions.
  7. martinbuberl renamed this gist Oct 25, 2016. 1 changed file with 0 additions and 0 deletions.
  8. martinbuberl renamed this gist Oct 25, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. martinbuberl renamed this gist Oct 25, 2016. 1 changed file with 0 additions and 0 deletions.
  10. martinbuberl renamed this gist Oct 25, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions readme.md → ...t existing Git repository into another.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Import existing Git repository into another

    Folder structure before (2 separate repositories):

    ```
  11. martinbuberl renamed this gist Oct 25, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. martinbuberl created this gist Oct 25, 2016.
    32 changes: 32 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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."
    ```

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