Skip to content

Instantly share code, notes, and snippets.

@77
Created December 21, 2022 18:10
Show Gist options
  • Save 77/4cc05f0c148758ce620bcf4d74350dbe to your computer and use it in GitHub Desktop.
Save 77/4cc05f0c148758ce620bcf4d74350dbe to your computer and use it in GitHub Desktop.
Move subdirectory to new git repo

HOWTO move a git subdirectory into a new repo using git-filter-repo

Install git-filter-repo

See the git-filter-repo repo for installation instructions.

Create a new repo on GitHub

Create a new empty repo on github.com and note the repo URL.

Mitgrate your subdirectory

First create a scratch directory to work in to avoid touching an existing repo.

 mkdir tmp_migration_dir

Next create a fresh clone of your existing repo with the directory you want to move.

 cd tmp_migration_dir
 gh repo clone old_repo_org/old_repo_name

Filter the repo to extract the subdirectory you want to move.

 cd old_repo_name
 git filter-repo --subdirectory-filter path/to/subdirectory/

Add a remote for your new repo and push to it.

git remote add origin https://github.com/new_repo_org/new_repo_name.git
git push -u origin master

Remove the temporary directory.

cd ../..
rmdir -Rf tmp_migration_dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment