DEV Community


Using Template Repositories w/ GitHub

Quinn Lashinsky
Front End Developer. Huge fan of Nintendo, baseball, cats, and fitness.
・3 min read

Abstractions are a key part of writing concise, useful code. When we create abstractions we write less code, reuse more code, and make our programs more clear and understandable. While working on building yet another server for a project idea I had, I realized I was coding the same boilerplate I had coded multiple times before. I was not abstracting this process effectively. How could I create a template repository (repo) with an already made repo?

The Do-It-Yourself Option

We can choose a repo to treat as a template. From there we have to decouple the template from its remote origin, and add a new remote origin repo to push to, creating our new project.

Let's walk through the steps:

First we'll go to the repo on GitHub you're going to treat as a template and copy the repo's git clone command.

The window that appears when you click the "Clone or download" button in the "Code" tab in your repository

In your command line, paste the command you copied. It should look something like this without the angle brackets ("<" and ">") :

git clone<your-username>/<your-template-repo-name>.git

From here, we can add the name of the folder we want this repo to clone into as the last argument of this command.

git clone<your-username>/<your-template-repo-name>.git <file-name>

If the file already exists, the repo will clone into that file. If it does not exist, the file will be created and then the repo will be cloned into it.

*Helpful Tip:

git remote -v

This command will tell you where your current repo points to as its origin

Then, we can change the origin url by running the below command. The origin url is the alias for a remote repo, which for our purposes, is the version of this repo stored on GitHub's servers.

git remote set-url origin<your-username>/<target-repo-for-new-project>.git

Now we're set! We've used a de facto template repo to create a new project repo structured just how we like.

The Automatic Way

We've just manually created a template repo. But is there a reason to manually handle this process each time you create a new template/project?

Luckily websites like GitLab and GitHub have added the ability to let you use any of your repo's as a template repo. Let's see how we can create a template repo on GitHub.

Go to Settings tab on the repo page you want to make a template repo.

GitHub repo on the Settings tab, indicating the ability to click a checkbox to enable a repository to become a template repository

Right below the name of the repo, we can click a checkbox to turn the current repo into a template repo.

GitHub repo on the Settings tab, with checkbox to enable a repository to become a template repository checked

That's all it takes!

Now whenever we create a new repo, we'll be given the option to use a template repo at the top of the page. This will create our repo with the base file structure defined in our template. From here we can clone the repo, and get to work.

Creating a new repo on GitHub and selecting the instant dark mode template for my new Vantablack website project

We'll even see a reference to the template repo below the heading that includes our GitHub username and repo name.

GitHub username, repo name and just below the text, "generated from QMaximillian/instant-dark-mode"

Creating a template repo can help you think about just how to to design the base architecture of any project you create. Reasoning about design decisions like using an ORM, SQL or NoSQL, or including authentication are all up to you. Once made, you can continually update the code to suit your needs. By creating a template repo we can abstract the work that is common across projects, and confidently create projects with shorter development time.

Discussion (0)