Skip to content

Instantly share code, notes, and snippets.

@davidonlaptop
Last active July 18, 2022 03:39
Show Gist options
  • Save davidonlaptop/36234fd9fa7f1a0f6b2cd5296216f280 to your computer and use it in GitHub Desktop.
Save davidonlaptop/36234fd9fa7f1a0f6b2cd5296216f280 to your computer and use it in GitHub Desktop.
Import git subfolder into another repository with history (2022)

1. Install git-filter-repo

brew install git-filter-repo
# or just download the single python file

2. Clone fresh copy of both of your repo (in case you mess up)

git clone git@github.com:YOUR_ORGANIZATION/repo1.git tmprepo1
git clone git@github.com:YOUR_ORGANIZATION/repo2.git tmprepo2

3. Rewrite the history on the local copy of the first repo to only include the subfolder you want

The single quotes are unnecessary, but make it clearer to a human that we are replacing the empty string as a prefix with my-module-

cd tmprepo1/
git filter-repo --path subfolder --tag-rename '':'subfolder-prefix-'
# or
git filter-repo --path subfolder --to-subdirectory-filter move-to-this-folder --tag-rename '':'subfolder-prefix-'

4. Import the branch with rewritten history into a new branch in destination repository

cd tmprepo2
git remote add tmpremote $HOME/workspace/tmprepo1
git fetch tmpremote
git checkout -b imported-branch tmpremote/main

5. Rebase on main branch, cleanup temporary remote, and push imported branch to destination remote

git rebase main
git remote remove tmpremote
git push -u origin imported-branch

Links

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