Methods for Converting a GitHub Repository to a Template for Reusable Boilerplate
You can convert any GitHub repository into a reusable template by enabling the "Template repository" toggle in the repository settings, which exposes a "Use this template" button and enables programmatic generation via the GitHub CLI or REST API.
Converting a GitHub repository to a template creates a standardized boilerplate that developers can instantiate without forking or manual file copying. The tiimgreen/github-cheat-sheet repository demonstrates this workflow, serving as a practical example of how to structure reusable project bases. This guide explains the mechanics of repository templates based on the source documentation found in README.md lines 886-896 and the repository's implementation patterns.
What Is a GitHub Repository Template?
A template repository is a standard GitHub repository that has the template=true flag enabled in its metadata. Once activated, GitHub automatically generates a template URL and renders a "Use this template" button on the repository page. When users click this button, GitHub creates a new repository containing an exact copy of the original file tree and directory structure, but with a fresh initial commit that contains none of the original git history.
According to the source code analysis of tiimgreen/github-cheat-sheet, this feature is documented in the Repository Templates section of README.md【/tmp/instagit_pzx3qj2b/README.md#L886-L896】.
Enabling the Template Flag
To convert an existing repository into a template, you must toggle the template flag in the repository settings:
- Navigate to your repository on GitHub.
- Click the Settings tab.
- Under the Features section, check the box labeled Template repository.
Once enabled, GitHub immediately updates the repository's metadata and exposes the template functionality through both the web interface and API endpoints.
Creating New Repositories from a Template
After enabling the template flag, you can generate new repositories through three primary interfaces. Each method performs the same server-side operation: copying the file tree from the default branch without history.
Via the GitHub Web UI
Click the "Use this template" button on the repository homepage, then select "Create new repository." You will be prompted to enter a name, choose visibility (public or private), and add an optional description. GitHub then performs a fork-style copy of the source files to your account or organization.
The UI flow is illustrated in the README images within tiimgreen/github-cheat-sheet【/tmp/instagit_pzx3qj2b/README.md#L891-L896】.
Via the GitHub CLI (gh)
The gh CLI provides the most efficient method for converting a GitHub repository to a template and generating instances from it:
# Create a new repository from a template
gh repo create my-new-project \
--template tiimgreen/github-cheat-sheet \
--public # or --private
This command sends a POST request to the GitHub API endpoint POST /repos/:owner/:repo/generate with a JSON payload containing the new repository's owner, name, and visibility settings.
Via the REST API
For automation scripts and CI/CD pipelines, use the REST API directly:
curl -X POST \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.baptiste-preview+json" \
https://api.github.com/repos/tiimgreen/github-cheat-sheet/generate \
-d '{"owner":"myorg","name":"my-new-project","description":"Repo generated from a template"}'
The generate endpoint reads the template flag from the source repository, replicates the file tree, and creates a fresh repository under the specified owner with a single initial commit.
What Gets Copied vs. What Gets Left Behind
When generating a repository from a template, GitHub performs a selective copy operation:
- Copied: All files in the default branch (including hidden files like
.gitignore), exact directory structure, and repository metadata files like.github/FUNDING.yml. - Not copied: Git commit history (new repos start with one initial commit), release assets, and branch protection rules.
- Partially copied: Submodule references are copied, but the submodule contents are not included in the generated repository.
This architecture ensures that new projects start clean without the baggage of development history, while maintaining the exact file structure needed for boilerplate consistency.
Best Practices for Template Design
To maximize the effectiveness of converting a GitHub repository to a template, consider these implementation details from the tiimgreen/github-cheat-sheet analysis:
- Avoid large binary assets: Files like
*.ziparchives increase repository size for every generated copy. Store large binaries in GitHub Releases or Git LFS instead of the template itself. - Never commit secrets: Template repositories copy files verbatim. Ensure
.envfiles, private keys, and credentials are excluded via.gitignorebefore enabling the template flag. - Validate submodules: Since submodule contents are not copied, verify that
.gitmodulesreferences point to stable, public upstream repositories. - Include multilingual documentation: The
github-cheat-sheetrepository includesREADME.zh-cn.md,README.ja.md,README.ko.md, andREADME.zh-tw.md, all of which are copied to generated repositories, providing immediate multilingual boilerplate support.
Summary
- Template repositories are created by enabling the "Template repository" flag in repository settings, which sets
template=truein the repository metadata. - Three generation methods exist: the Web UI "Use this template" button, the
gh repo create --templateCLI command, and thePOST /repos/:owner/:repo/generateREST API endpoint. - File-only copying occurs during generation: the new repository receives the complete file tree from the default branch but no git history, releases, or branch protection rules.
- Security consideration: Never include secrets or sensitive configuration in template repositories, as they are copied verbatim to every generated instance.
Frequently Asked Questions
Does converting a repository to a template preserve git history?
No. When you generate a new repository from a template, GitHub creates a single initial commit containing the copied files from the default branch. The original commit history, tags, and pull request history are not transferred to the new repository.
Can I update repositories that were previously created from a template?
No. Generated repositories are independent copies with no upstream linkage to the template. Unlike forks, they do not receive updates when the template changes. To propagate updates, you must manually merge changes or recreate the repository from the updated template.
What happens to Git submodules when using a template repository?
The .gitmodules file and submodule references are copied to the new repository, but the actual submodule contents are not included. You must run git submodule update --init after cloning the generated repository to fetch submodule data.
Is there a size limit for template repositories?
While GitHub does not publish specific size limits for templates, repositories containing large binary files will cause slower generation times and larger clones for end users. Keep template repositories under 100MB by excluding build artifacts, media files, and dependencies, using .gitignore rules and GitHub Releases for large assets instead.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →