Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Rewriting a repo to contain the history of just specific files#
I wanted to start a new git repository containing just the history of two specific files from my help-scraper repository.
I started out planning to use git filter-branch for this, but got put off when this StackOverflow thread started talking about the need to understand the differences between macOS sed and regular GNU sed.
That thread also pointed me to git-filter-repo, a really neat Python script that makes this so much easier.
Installing git-filter-repo#
git-filter-repo is written in Python but has zero dependencies on anything else - all you need to do is place the script somewhere on your path.
I ran echo $PATH to check which directories were on my path - one of them is .local/bin - so I decided to put it there:
1cd ~/.local/bin2wget https://raw.githubusercontent.com/newren/git-filter-repo/main/git-filter-repo3chmod 755 git-filter-repoIt didn’t work until I ran chmod 755 on it.
Now I can run this:
1% git filter-repo2No arguments specified.Confirming the new command is installed!
Rewriting my repository#
The --path option can be used to preserve just the history of specified paths. I ran this:
1cd /tmp2git clone https://github.com/simonw/help-scraper3cd help-scraper4git filter-repo --path flyctl/fly.graphql --path github/github.graphqlThe command output was:
1Parsed 132 commits2New history written in 0.33 seconds; now repacking/cleaning...3Repacking your repo and cleaning out old unneeded objects4HEAD is now at 828a1efc GitHub: Tue Mar 22 15:09:04 UTC 20225Enumerating objects: 144, done.6Counting objects: 100% (144/144), done.7Delta compression using up to 12 threads8Compressing objects: 100% (69/69), done.9Writing objects: 100% (144/144), done.10Total 144 (delta 34), reused 55 (delta 34), pack-reused 011Completely finished after 1.33 seconds.And sure enough, my repo now only contains those two files:
1% find . | grep -v .git2.3./flyctl4./flyctl/fly.graphqlAnd the commit history for those files:
1% git log --stat --pretty=oneline | head -n 102828a1efc4307cca6cd378c394c2d33eac2eceb52 GitHub: Tue Mar 22 15:09:04 UTC 20223 github/github.graphql | 27 +++++++++++++++++++++++++++4 1 file changed, 27 insertions(+)544fc9ebb49a255ad1c90f7761ab2ab1267135ff9 GitHub: Fri Mar 18 15:08:38 UTC 20226 github/github.graphql | 1 +7 1 file changed, 1 insertion(+)8dae52d5b94761145333867bc6641e8210409c3b7 GitHub: Thu Mar 17 16:58:45 UTC 20229 github/github.graphql | 3 +++10 1 file changed, 3 insertions(+)1133e0691bc579bb1e26ea6d05556cf047f5c120da GitHub: Wed Mar 16 15:05:35 UTC 202212help-scraper % git log --stat --pretty=oneline | head -n 2013828a1efc4307cca6cd378c394c2d33eac2eceb52 GitHub: Tue Mar 22 15:09:04 UTC 202214 github/github.graphql | 27 +++++++++++++++++++++++++++15 1 file changed, 27 insertions(+)1644fc9ebb49a255ad1c90f7761ab2ab1267135ff9 GitHub: Fri Mar 18 15:08:38 UTC 202217 github/github.graphql | 1 +18 1 file changed, 1 insertion(+)19dae52d5b94761145333867bc6641e8210409c3b7 GitHub: Thu Mar 17 16:58:45 UTC 202220 github/github.graphql | 3 +++21 1 file changed, 3 insertions(+)2233e0691bc579bb1e26ea6d05556cf047f5c120da GitHub: Wed Mar 16 15:05:35 UTC 202223 github/github.graphql | 54 ++++++++++++++++++++++++++++++++++++++++++++++++---24 1 file changed, 51 insertions(+), 3 deletions(-)253dae87045436c3a8c6c84409f9e7ec5ae3bfae74 GitHub: Tue Mar 15 15:08:59 UTC 202226 github/github.graphql | 18 ++++++++++++++++++27 1 file changed, 18 insertions(+)2874a3f34c254f789948ee0257981f5d30a094d19f Fly: Tue Mar 15 06:23:03 UTC 202229 flyctl/fly.graphql | 16 ++++++++++++++++30 1 file changed, 16 insertions(+)3163c92c60285b08a898b874b9296b28935f0e7ea8 GitHub: Mon Mar 14 15:09:42 UTC 202232 github/github.graphql | 5 ++++-