Git Filter Repo — Splitting a Subfolder Into A New Repository
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
- Install git-filter-repo
brew install git-filter-repo
2. Change directories to the location where you would like to create the new frontend repository
cd ~
3. Clone down the original PROJECT repository and change directories
git clone git@github.com:[PROJECT]cd PROJECT
4. Run git filter-repo
git filter-repo --subdirectory-filter FRONTEND/ --tag-rename '':'frontend'
5. Create FRONTEND repository in GitHub
6. Add the remote branch
cd FRONTENDgit remote add origin git@github.com:[FRONTEND]git branch -M maingit push -u origin main
7. Push all git branches to the remote
git push remote --all
And that’s it! I hope you found this post helpful and thank you for reading.
Resources
Originally published at https://www.edezekiel.com.