Git Filter Repo

Welcome to 2021! This is my first blog post of the new year and I'm excited to get back to writing. Let's dive right in.

The Task: Pulling a Frontend Repository From a Combined Frontend/Backend Project

I've worked on a project for some time that has one GitHub repository containing both the frontend and backend applications.

PROJECT/
    .git/
    BACKEND/
    FRONTEND/

Recently, it finally came time to split these applications apart:

BACKEND/
    .git/
    APP/
FRONTEND/
    .git/
    APP/    

Preserving Git History

One obviously problem with creating a new repository from a subfolder is preserving git history. Losing years of git history or losing current branches in development would have been a non-starter.

This post shares the exact steps I used to create a new frontend repository from the project while preserving git history. The key here was a tool called git filter-repo. The git CLI itself advised me to use git filter repo instead of the native git filter-branch command.

Instructions

  1. Install git-filter-repo
brew install git-filter-repo
  1. Change directories to the location where you would like to create the new frontend repository
cd ~
  1. Clone down the original PROJECT repository and change directories
git clone git@github.com:[PROJECT]

cd PROJECT
  1. Run git filter-repo
git filter-repo --subdirectory-filter FRONTEND/ --tag-rename '':'frontend'
  1. Create FRONTEND repository in GitHub

  2. Add the remote branch

cd FRONTEND

git remote add origin git@github.com:[FRONTEND]

git branch -M main

git push -u origin main
  1. Push all git branches to the remote
git push remote --all

And that's it! I hope you found this post useful and thank you for reading.

Resources