continue development ← panda-puss → bare clone (local) → pp-template →mirror push (remote) → pp-template → new

Duplicating a git repository to create a template repository

I find out how to duplicate a repository (repo) in GitHub to create a template for other projects. Turns out it’s straightforward.

The scenario

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?

The palaver

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:

General (settings)

Repository name: panda-puss

Check box (not ticked). Template repository.
Template repositories let users generate new repositories with the same directory structure and files.
It’s easy to convert a GitHub repository into a template

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:

1) continue development ← panda-puss → pp-fork → pp-template → new

2) new ← pp-template ← rename ← panda-puss → pp-fork → rename → panda-puss → continue development

3) panda-puss → rename → pp-template → panda-puss, new
Ideas 1, 2 and 3 for how to generate a template repository from panda-puss
where: pp-fork is a repository forked from panda-puss and pp-template is the template repository

Forking repositories

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:

Tooltip: 'Cannot fork because you own this repository and are not a member of any organizations.'
You can’t fork 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.

Renaming repositories

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?

Duplicating repositories

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:

  1. Make a local bare clone from the remote repo.
  2. Create a new repo on the server.
  3. Mirror-push the local bare clone to the new repo on the server.
  4. Delete the local bare clone.
continue development ← panda-puss → bare clone (local) → pp-template →mirror push (remote) → pp-template →new
How to create a template repository from an existing repository
where pp-template is the template repository

Then all that has to be done is to set the new repo to be a template, and that’s it: done.

The solution

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:

  1. 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
  2. Create a new repo on the server as described by the GitHub document Creating a new repository.
  3. 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
  4. Delete the local bare clone:
    cd ..rm -rf original-repository.git
  5. Select the template repo option in GitHub’s settings (assuming you’re looking at your repo on the GitHub website):
    Settings > General > Template repository
    General (settings)

Repository name: panda-wp_theme_template

Check box (ticked). Template repository.
Template repositories let users generate new repositories with the same directory structure and files.
    Tick the Template repository box

All done!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.