How I Moved a Subfolder to a New GitHub Repo With Full Git History

Jayesh VermaJayesh Verma
3 min read

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 to git 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! πŸš€

0
Subscribe to my newsletter

Read articles from Jayesh Verma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jayesh Verma
Jayesh Verma