How I Moved a Subfolder to a New GitHub Repo With Full Git History
Recently, I needed to move a subfolder named Day4 Backend
from one of my large GitHub repositories (Backend-Practice
) into a separate repository, while preserving the entire commit history of that folder.
Turns out, this is totally possible β and actually quite easy using a powerful tool called git filter-repo
.
Hereβs how I did it π
π§° Tools Youβll Need
- Git installed on your system
-
git filter-repo
β a faster and safer alternative togit filter-branch
π‘ Install
git filter-repo
using pip:pip install git-filter-repo
π Scenario
Source repository:
https://github.com/Jv2350/Backend-Practice
Subfolder to extract:
Day4 Backend
Target repository:
https://github.com/Jv2350/Video-Hosting-Website
My goal was to move only Day4 Backend
into a new repository with all its commits intact.
π Step-by-Step Guide
1οΈβ£ Clone the original repository
git clone https://github.com/Jv2350/Backend-Practice.git Video-Hosting-Website
cd Video-Hosting-Website
2οΈβ£ Extract the subfolder using git filter-repo
git filter-repo --subdirectory-filter "Day4 Backend"
β This will:
- Remove everything except the Day4 Backend folder
- Move its contents to the root
- Retain only the commits that touched that folder
3οΈβ£ Create a new GitHub repo
Go to GitHub and create a new repo, e.g., Video-Hosting-Website
.
Then add it as a remote:
git remote add origin https://github.com/Jv2350/Video-Hosting-Website.git
4οΈβ£ Push your clean history
git push -u origin main
β Final Result
Now your new repository will:
- Contain only the contents of the Day4 Backend folder
- Have a clean project root
- Retain all commit history related to that folder only
π Here's my result:
π github.com/Jv2350/Video-Hosting-Website
π Common Errors & Fixes
β No such remote: 'origin'
After using git filter-repo
, the default remote is removed.
π Fix: Just skip git remote rename
and directly add a new remote with:
git remote add origin <url>
β Spaces in URLs
Make sure your GitHub repo name doesnβt contain spaces. Use hyphens instead, like:
https://github.com/Jv2350/Video-Hosting-Website.git
π§Ό Bonus: Clean Up
Now that your repo is split and clean, consider:
- Adding a
README.md
- Creating a
.gitignore
- Setting up a new
LICENSE
π¬ Final Thoughts
Splitting a subfolder into a new GitHub repo without losing history is easier than it sounds β and git filter-repo
makes it lightning fast β‘
This method helped me clean up my projects, improve modularity, and organize my work better.
I hope this helps you too! π
If you found it useful, drop a comment or share it with someone who works with Git π»
Happy hacking! π
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.