13

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
  • 638
  • 6
  • 18
  • 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

2 Answers2

14

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,129,465
  • 480
  • 4,036
  • 4,755
  • 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
10

Try gh repo create myrepo --template someuser/sometemplate

Ben Gubler
  • 1,241
  • 1
  • 15
  • 30
  • 1
    True, that is a possibility since Aug. 2020 and gh v0.12.0. Upvoted. I have updated my answer accordingly. – VonC Sep 16 '20 at 14:44