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?
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.
In your command line, paste the command you copied. It should look something like this without the angle brackets ("<" and ">") :
git clone firstname.lastname@example.org:<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 email@example.com:<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.
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 firstname.lastname@example.org:<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.
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.
Right below the name of the repo, we can click a checkbox to turn the current repo into a template repo.
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.
We'll even see a reference to the template repo below the heading that includes our GitHub username and repo name.
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.