Fly, Penguin!

I blog so I don't forget.

extract parts of a git repo as new one

1 minute read #git #cli

Situation: 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, …

  • only keep the parts of all commits which concern files under ./my/repo/part/**
  • remove the path prefix ./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.