On this page:
I find out how to duplicate a repository (repo) in GitHub to create a template for other projects. Turns out it’s straightforward.
My Panda-Puss WordPress (WP) block theme has come along in leaps and bounds recently, after fighting WP about almost everything. I have focused on the layout, keeping most styling matters (e.g. colours and fonts) for later. I’m saving all the changes in a GitHub repo called panda-puss.
It dawned on me that I might like to use the Panda-Puss WP theme as a base for other themes, so I wanted to create a copy of my panda-puss repo that I could use like a template, but without affecting the panda-puss repo itself. But how to do it?
I turned to the internet (of course) to see if anything like what I wanted was possible.
Creating template repositories
I discovered that GitHub introduced template repos in June 2019, so part of what I wanted was possible. There’s a simple switch in the repo’s general settings that allows you to toggle that repo into a template and back again:
I was delighted to find that it would be easy to set a repo as a template for new WP themes. However, I didn’t want the WP theme template repo to be called panda-puss; I wanted to rename panda-puss to something generic like panda-wp_theme_template, convert panda-wp_theme_template to a template, then creating a new repo based on panda-wp_theme_template called panda-puss. I’d need some way of creating a new repo based on panda-puss.
Creating a second repository
I considered three possibilities, two of which involved forking panda-puss and one of which involved a simple rename of panda-puss:
My first idea involving forks was to create a fork and convert that fork into a template; I’d continue development of panda-puss as if nothing had happened.
My second idea involving forks was create a fork and use that as panda-puss; the original repo would become the template repo. This one potentially involves renaming the repos.
I looked at GitHub’s documentation on forking repos; it says that
forks are used to either propose changes to someone else’s project to which you don’t have write access, or to use someone else’s project as a starting point for your own idea. I’d be forking my own repo; what difference would it make?
It turns out it makes a lot of difference: GitHub’s interface allows you only to fork other people’s repositories, as indicated by the tooltip that appears over the greyed-out fork button in your own repository:
That meant both of these options were impossible. Time to look at the third option: simply renaming the repository and using it as a template from which to derive a new panda-puss repo.
When I looked into renaming repos, I found that the GitHub documentation on renaming a repo specifically warns against doing what I wanted to do:
Warning: If you create a new repository under your account in the future, do not reuse the original name of the renamed repository. If you do, redirects to the renamed repository will break.
Renaming a repository, GitHub Docs
So much for a simple rename.
I came up with another idea: would it be possible to make a copy of a repo and use the copy as the template?
After some more rummaging around the internet, I discovered that you can indeed duplicate a repository, and that it’s not even difficult to do, although it seems a bit scary. I created a repo on which to test this before I did it for real to make absolutely sure it would do what I thought it would do.
All you have to do is:
- Make a local bare clone from the remote repo.
- Create a new repo on the server.
- Mirror-push the local bare clone to the new repo on the server.
- Delete the local bare clone.
Then all that has to be done is to set the new repo to be a template, and that’s it: done.
To create a copy of my panda-puss repo and convert it to a template repo, I performed the following steps, where username is my username, original-repository is my panda-puss repo and new-repository is panda-wp_theme_template:
- In the command-line interface, make a local bare clone from the remote repo (it doesn’t matter where you do this because git creates a new directory, which you’re going to delete when you’re done):
git clone --bare https://github.com/username/original-repository.git
- Create a new repo on the server as described by the GitHub document Creating a new repository.
- Mirror-push the local bare clone to the new repo on the server:
cd original-repository.gitgit push --mirror https://github.com/exampleuser/new-repository.git
- Delete the local bare clone:
cd ..rm -rf original-repository.git
- Select the template repo option in GitHub’s settings (assuming you’re looking at your repo on the GitHub website):
Settings > General > Template repository