Mastodon Mastodon - Extract parts of a git repo as new one
 logo
  • Home 
  • Tags 
  • Blog posts 
  1. Home
  2. Blog posts
  3. Extract parts of a git repo as new one

Extract parts of a git repo as new one

Posted on August 21, 2021  (Last modified on July 11, 2024) • 1 min read • 156 words
Git   Cli  
Git   Cli  
Share via

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.

 AWS managed AD - first steps after creation
jq snippets 
In case you want to follow me

Here are some links. The further to the right, the less active.

           
(c) Axel Bock | Powered by Hinode.
Link copied to clipboard
Code copied to clipboard