Skip to main content

Move a subdirectory into its own Git repo, with history

I moved a project that was in a subdirectory of a larger, pretty much unrelated, repository. I don’t know who thought it belonged there to begin with, but it was my first go at pulling something like that out into its own repo.

Everything below is for Windows, but the instructions shouldn’t vary too much for other OS’s. YMMV and all that.

  1. Follow steps 1-4 in GitHub’s tutorial, but pause there. The filter-branch command in step 5 must be pretty awful, because even the docs advise you to use something else.. so we will.
  2. Install Python3 if you don’t already have it.
  3. Copy the git-filter-repo file from this repo, and save it somewhere in your path, so you can access it easily from the command line in like… 30 seconds.
  4. According to the notes for using it on Windows, you may have to change the first line of the file from python3 to python. I did.
  5. Run git filter-repo --subdirectory-filter location/of/subfolder. I had to use the --force command due to some error it was throwing about my repo not being a new clone, even though it was. Didn’t harm anything though.
  6. Pick back up with steps 6-10 in GitHub’s tutorial. Step 8 returned nothing for me, and step 9 failed, so I ran git remote add origin https://location_of_your_new_repo instead, and then verified it with the command in step 10.
  7. Instead of step 11, I ran git push -u origin --all.

The end result was a new repo, with the previous subdirectory as the root of the new repo, complete with all branches and history. Success! 🎉

Related

How to Create a Git Alias

If you’re unfamiliar with Git’s “alias” feature, it provides a way to create shortcuts for other Git commands, which can save you a lot of time. They’re easy to setup and maintain too. Let’s see how.

Deploy your own RequestBin in under 5 minutes

If you need to consume a webhook from another service, or verify the payload being sent from your own REST API endpoint, RequestBin can help. It intercepts and displays the contents of any call made to it. Here’s how to deploy your own instance in just a few minutes.