17

Creating a new repo using another as a template is a great function, but I can only see how to use this ability on github.com. Can it be done entirely from command line? Perhaps without a remote?

I would like to use templates for a user to intialise a repo to store their secrets in, and it may not even require a remote, but its very important that it isn't connected to the original template repo for privacy. The .gitignore file and the folder tree provided are the most important functions that I'm hoping to provide the user with this ability.

openCivilisation
  • 796
  • 1
  • 8
  • 25
  • 1
    Isn't "create a repo from a template" effectively the same as "clone that repo, then change this clone's remote"? Or when you create from a template, is the history "fresh"? I'm not that familiar with GH's templates concept. – r2evans Jun 29 '20 at 02:55
  • (Answered my own question, https://help.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template. No, commit-history is not transferred with a template-copy.) – r2evans Jun 29 '20 at 02:57
  • 1
    @r2evans if you don't get commit history but the files, then it's just copy and initialise. Or clone, delete the .git folder and initialise. – VLAZ Jun 29 '20 at 05:21
  • Interestingly, I'm trying to clone from a template, while keeping history, but I don't want it to be considered as a fork (PR should not go to the upstream), so while it's slightly different from what OP is asking, I don't see how templates can help since they wipe the history. – Vadorequest Oct 23 '22 at 14:44

4 Answers4

20

June 2020: Since a template repository such as this one is a GitHub repository, you can:

That way, everything is done form the command line.


Update Sept. 2020: the other approach through the GitHub CLI tool gh, and mentions in Ben Gubler's answer, stems from PR 1590: "Create repositories from a template repo" from Mislav Marohnić and Colin Shum.
(merged in commit 99372f0)

gh repo create <new-repo-name> --template="<link-to-template-repo>"
# OR
gh repo create <new-repo-name> --template="<owner/template-repo>"
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Is it possible for the CLI to use a specific `branch` from the template? It looks [possible to do (include all branches) from the UI](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-from-a-template), but I'm not sure if there is a hidden flag on the CLI... – tdensmore Apr 23 '21 at 19:07
  • @tdensmore I don't think so: you would need to change the default branch of the default init branch: (https://stackoverflow.com/a/65376126/6309) of said template repository, and then call `gh repo create --template` as usual. – VonC Apr 23 '21 at 20:17
11

Try gh repo create myrepo --template someuser/sometemplate

Ben Gubler
  • 1,393
  • 3
  • 18
  • 32
1

The existing answers are great for simple use cases, but I wanted to do something a bit more specific to my use-case:

  • Reuse an existing repo (GitHub template)
  • Keep the history of the source
  • Clone locally + create repo on GitHub
  • Configure the new repo automatically (private, default description, enable squash, ...)
  • Configure the remote and fetch branches from all remotes (to keep the new project up-to-date with the template easily)
  • Through a single command that I can quickly adapt to deploy new projects

Here is what I came up with:

cd ~/dev \
&& gh repo create NEW_REPO \
--template OWNER/SOURCE_REPO \
--private \
--clone \
&& cd NEW_REPO \
&& gh repo edit OWNER/NEW_REPO \
--delete-branch-on-merge \
--description "Default description for NEW_REPO." \
--enable-squash-merge \
&& gh repo sync OWNER/NEW_REPO \
--source OWNER/SOURCE_REPO \
--force \
&& git remote add template git@github.com:OWNER/SOURCE_REPO.git \
&& git fetch --all

Note that I first change directory to ~/dev, you might want to change that.

Vadorequest
  • 16,593
  • 24
  • 118
  • 215
0

Create your template repo. Put it (the contents of the .git dir if it's not bare) somewhere they can reach with a filesystem path. Clean out anything you don't want. Then they can git init --template=/path/to/that/template.

Since the template directory isn't named .git you can even make a repository to track it and publish that, then people can clone your template and use its work tree as a template.

jthill
  • 55,082
  • 5
  • 77
  • 137