Skip to content

Instantly share code, notes, and snippets.

@miloszwasacz
Last active September 28, 2025 00:53
Show Gist options
  • Select an option

  • Save miloszwasacz/3a18d8521b2e2339c87655af872cc432 to your computer and use it in GitHub Desktop.

Select an option

Save miloszwasacz/3a18d8521b2e2339c87655af872cc432 to your computer and use it in GitHub Desktop.
Put a git repo as a subfolder in another git repo

This is a short guide on how to put all files from a Git repository into a folder in another Git repository.

  1. Go the subrepo (REPO1), switch to the branch you want to merge, and make a new temporary branch.

    cd REPO1
    git checkout BRANCH_TO_MERGE
    git checkout -b TEMP_BRANCH
  2. Move the files you want to relocate into a subdirectory within the same repository.

    mkdir TARGET_SUBFOLDER
    git mv * TARGET_SUBFOLDER
    git commit -m "Move files to the target subfolder"

    The file structure has to represent what the target repo should look like after the merge (only in terms of the merged subrepo).

  3. Create a bundle of the branch that contains the moved files.

    git bundle create ../repo1_bundle TEMP_BRANCH
  4. In your target repo (REPO2), fetch the bundle and create a new branch from it.

    cd ../REPO2
    git fetch ../repo1_bundle TEMP_BRANCH:NEW_BRANCH
  5. Merge the changes from REPO1 into REPO2.

    git checkout master
    git merge NEW_BRANCH --allow-unrelated-histories
  6. Clean up by removing the temporary branch and bundle.

    # Clean up REPO2
    git branch -d NEW_BRANCH
    rm ../repo1_bundle
    
    # Clean up REPO1
    cd ../REPO1
    git checkout BRANCH_TO_MERGE
    git branch -d TEMP_BRANCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment