Extract parts of a git repo as new one
Posted on August 21, 2021 (Last modified on July 11, 2024) • 1 min read • 156 wordsSituation: You have a repository with a subdirectory my/repo/part
, which became large and own-project-worthy. You can copy the files into a new repo, but you’ll lose all git history.
What would be just perfect is: From all commits ever made in this repository, …
./my/repo/part/**
./my/repo/part/
.So, basically, rewrite and filter all git commits.
There’s an app for that:
git-filter-repo
. And that’s how you use it:
# clone the repository parallel to the existing one
git clone git@github.com:me-and/my-repo fool-around-here
cd fool-around-here
# remove all other files except the ones you want
# (yup, it's that easy)
git-filter-repo --path my/repo/part
# move all files to the repository root
git-filter-repo --subdirectory-filter my/repo/part
# set a new origin URL
git remote set-url origin git@github.com:me-and/my-new-empty-prepared-repo
# push.
git push
The git history has been rewritten 1984-style. For git it has always been exactly like this now.