Skip to content
Edit this page

git tpl init

Rendered-ref publication is conditional on its original tip, including expected absence. A concurrent writer produces tpl::git::stale_ref; inspect the current ref and retry. The failed publication does not save the attachment configuration.

Attach a template to a repository, render it, and merge the result.

All attachments require an index matching HEAD (empty on an unborn branch) and no active Git operation. This includes --no-merge, which still creates an attachment configuration commit. Commit or unstage staged changes first; --no-merge preserves unrelated unstaged edits.

git tpl init <template> [<dir>] [--ref <ref>] [--answer k=v]... [options]

What it does

  1. Discovers a Git repository at or above the destination — <dir>, or the current directory if omitted. With --create, creates missing directories and initializes a repository only if discovery finds none. Rendering and attachment use the discovered repository root, even when the destination is a subdirectory.
  2. Resolves the template source and its revision.
  3. Loads the template manifest and any data sources it needs.
  4. Builds the dependency graph and validates it.
  5. Asks the questions, in dependency order.
  6. Resolves computed values.
  7. Renders the template.
  8. Writes .config/git.tpl.toml. A template that fails to render leaves no half-initialised project behind, which is why this comes after the rendering rather than before it.
  9. Creates refs/tpl/<id> as an orphan commit when no prior rendered ref exists. With --force on an existing ref, its previous tip is the parent, preserving the established merge base.
  10. Merges that commit into the current branch, allowing unrelated histories, with .config/git.tpl.toml in the merge commit itself. One init, one commit on your branch — see ADR-021.
  11. Adds any [remotes] the template declared.
  12. Shows the template's own note, if it has one.

Steps 11 and 12 come last, after everything git-tpl did itself, and happen on init only. git tpl update does neither: it is a ref-only operation, and that is most of its value.

A template, ancestor, or selected composed child can declare a minimum_version. An incompatible engine stops before publishing rendered files, attachment configuration, commits, refs, or merges. With --create, the initial empty directory and repository may already have been created. Upgrade git-tpl to the version reported in the diagnostic and retry.

What a template may and may not do afterwards

It may add a Git remote, and it may say something to you. That is the whole list, and it is closed — ADR-019 states the bar for anything joining it.

It may not run anything. There are no hooks, no scripts and no post-render commands, on init or ever. A template that needs npm install renders a scripts/bootstrap.sh and now, at last, has a way to tell you it is there; you run it. A note that says "run curl … | sh" is exactly as trustworthy as a README.md that says it.

A note is shown in a block attributed to the template, so it cannot be mistaken for git-tpl's own output, and it is stripped of everything a terminal would act on beyond colour and an https link. Note display reads from the template repository and creates no project file by itself. A note file inside the render root is also rendered into the project under the ordinary file-rendering rules. Keep it outside the render root if it should only be displayed.

Notes are rendered by Termimad when colour is enabled, with styled headings, emphasis, code, unordered lists and tables. Links, images, ordered lists and HTML remain source text. Code blocks have uniform styling, without language-aware syntax highlighting. When colour is disabled, the sanitised Markdown source is shown instead. This follows the normal colour policy, including --color always through a pipe and --color never on a terminal. The --json note remains the raw, evaluated text.

A note_file naming nothing is refused, and refused early: the note is resolved before the rendered ref is created and before the merge, so a template with a wrong path leaves your repository exactly as it found it.

An existing remote is never repointed. If the template declares an origin and you already have one somewhere else, yours stays and a warning names both.

The merge is the point

Step 10 is not a convenience. Without it the template commit is not an ancestor of your branch, so the first git tpl update would have no merge base — and without one, Git cannot tell your edits from the template's. Every file that differs would conflict, including files you customised that the template never changed.

---
config:
    theme: base
    # Zensical overrides classless text; inherit Mermaid's per-branch label colors instead.
    themeCSS: ".branchLabel text { fill: inherit !important; }"
---
gitGraph
    accTitle: Initial attachment merges unrelated histories
    accDescr: Merge M has two parents, the existing project commit A and the orphan rendered commit G0.
    %% Create both lanes before any commit so G0 has no parent.
    branch "refs/tpl/foo"
    checkout main
    commit id: "A"
    checkout "refs/tpl/foo"
    commit id: "G0"
    checkout main
    merge "refs/tpl/foo" id: "M"

git log --graph shows the template entering your history as a merged parent, which is exactly what it is.

M carries .config/git.tpl.toml, so the attachment is one commit and git show HEAD shows all of it. Two cases cannot be folded that way, because they produce no merge commit to carry the file, and it gets a chore(tpl): attach … commit of its own instead:

  • an empty repository, where the merge fast-forwards and G0 becomes the branch — G0 is the ref tip and must stay byte-identical to the rendering;
  • --no-merge.

A merge that conflicts commits nothing: the configuration is left staged, for the resolution commit you are about to make.

Use --no-merge to skip the rendered-output merge and perform it yourself. The attachment configuration still gets its own commit on your current branch, advancing HEAD.

<template> is any Git URL or local path. If you have defined shortcuts, a leading <name>: is expanded first — and it is the expanded URL that is recorded in the project, so the shortcut never leaves your machine.

Options

Option Meaning
<dir> Where to start upward repository discovery. Defaults to the current directory. Rendering uses the discovered root. Template paths and --answers-from remain invocation-relative.
--ref <ref> Branch, tag or commit. Defaults to a local template's checked-out branch, or a URL's remote default branch.
--create Create missing directories; initialize a repository only if none is discoverable at or above the destination.
--answer k=v Supply an answer, skipping its prompt. Repeatable.
--answers-from <path> Read answers from a TOML, JSON or YAML file. Repeatable. See Answers from a file.
--defaults Accept every default without prompting.
--strict-answers Fail on an answer that names no question, rather than warning.
--trust Fetch remote data sources without confirming. Per invocation; nothing is recorded.
--id <id> Override the derived template id, and so the ref name.
--force Re-ask the questions and re-render over an existing attachment.
--no-merge Create the ref, do not merge it.
--dirty Render the template's working tree rather than its HEAD. Local templates only.
--dry-run Report what would be asked and rendered; create nothing.

--ref and --dirty are mutually exclusive — asking for both is refused at the parser.

Example

$ git tpl init https://github.com/noirbizarre/rust-library-template

? Project name › my-project
? License › MIT
? Enable CI? › yes

Template:  https://github.com/noirbizarre/rust-library-template
Revision:  v1.4.0 (8b3e7d1)

Created refs/tpl/github-com-noirbizarre-rust-library-template

  added     Cargo.toml
  added     README.md
  added     src/lib.rs
  added     .github/workflows/ci.yml

Merged into main.

  Answers recorded in .config/git.tpl.toml and committed.

Template is the recorded source after any shortcut expansion, not the manifest's name: it is what .config/git.tpl.toml will record, and what a later --ref or --id will be resolved against.

Starting from nothing

--create replaces the former --init flag, which is no longer accepted. Update existing scripts to use --create.

Outside any existing repository, <dir> plus --create creates the directory and repository and attaches the template:

$ git tpl init https://github.com/noirbizarre/rust-library-template my-project --create

Repository discovery searches upward, including with --create. Inside an existing repository, this command attaches at that repository's root rather than creating a nested repository. To create an independent project there, initialize its repository explicitly first:

git init my-project
git tpl init https://github.com/noirbizarre/rust-library-template my-project

Without --create, a missing destination is refused. An existing destination is accepted when a repository can be discovered there or in a parent directory.

Preconditions

A Git repository discoverable at or above the destination — the current directory by default, or <dir> if given. init needs somewhere to put a ref. Pass --create to create missing directories and initialize a repository when none is discoverable. Explicitly run git init <dir> first when you intend an independent repository inside another repository. --dry-run is exempt: it creates nothing, so it previews against the template alone, even against a destination that does not exist yet or is not a Git repository.

No existing .config/git.tpl.toml. A project has one template. Re-running init is refused with tpl::ops::already_initialised, because it would silently discard the recorded answers. To take a newer template revision with the answers you already gave, run git tpl update. To re-run the full questionnaire, run git tpl init <template> --force, which re-asks every question and re-renders over the existing attachment. For targeted changes, use git tpl update --answer key=value or --answers-from answers.toml; these override the selected recorded answers and save them while preserving the others. Editing [answers] in .config/git.tpl.toml and running update works too.

A clean index and no active Git operation, including with --no-merge, because attachment writes a commit. Normal integration into a repository with existing commits also requires a clean worktree. --no-merge preserves unrelated unstaged edits while committing the attachment configuration.

An empty repository

init in a repository with no commits works: integration fast-forwards the branch to the orphan template commit. It then creates a separate attachment commit containing .config/git.tpl.toml:

refs/tpl/<id>:  G0
main:          G0 → attachment configuration

The rendered ref stays at G0, whose tree contains only rendered output. The completed initialization leaves HEAD at the attachment commit.

An existing project

Dry runs honor explicit --trust and persistent trust during ancestor resolution. With --defaults, a dry run renders the resolved revision and reports any render failure as an error. Without --defaults, it reports only the schema and JSON files is null. Schema and output use one resolved template revision. The preview selects the requested template ID's ref, including an explicit --id, and retains that ref's initialized one-shot files even if the source root has disappeared. It does not borrow the existing attachment's baseline when targeting a different ID, and an existing target ref works even without an attachment configuration. With --defaults, it evaluates init's note and remote metadata, rather than update's migration metadata. It does not check integration preconditions or create destinations, publish refs, save configuration, or modify HEAD, the index or worktree.

init also works on a project that already has files — one generated last year by another tool, or written by hand and since grown to resemble a template. There is no separate command, because there is no separate behaviour: the orphan commit is merged, and Git reconciles the two sides.

cd my-existing-project
git tpl init https://github.com/noirbizarre/rust-library-template

Expect conflicts, and expect them to be small. Because there is no merge base, Git compares the two sides by content:

Your project What happens
A file identical to the rendered one Merged silently. Nothing to reconcile.
A file that differs Conflicts only on the lines that differ, not the whole file.
A file the template renders and you lack Added and staged for you.
A file the template does not render Untouched. It is not the template's.

So a README.md you changed one line of comes back as a three-line conflict, not a page of markers.

Resolve them as you would any merge — this is ordinary Git, and git-tpl contributes no conflict resolution of its own (ADR-002):

$ git tpl init ../rust-library-template --defaults

Template:  ../rust-library-template
Revision:  main (7fa834c)

Created refs/tpl/rust-library-template

  added     Cargo.toml
  added     README.md
  added     src/lib.rs

warning: the merge left conflicts. Resolve them and commit:

  README.md

  git status
  git commit

  Answers recorded in .config/git.tpl.toml and staged.

$ git mergetool          # or edit the markers by hand
$ git commit

That first merge is the only awkward one. Afterwards G0 is an ancestor of your branch, so every git tpl update from then on is a small diff against a genuine merge base — the same experience as a project generated from the template on day one.

Look before you merge

git tpl init <template> --no-merge creates the ref without merging, so git tpl diff can preview reconciliation. The preview does not execute merge drivers; native integration can produce different results. When you are ready, git tpl merge performs it. --dry-run is smaller still: it reports what would be asked and rendered, and creates nothing.

Machine-readable output

The displayed Template and JSON template values use the source spelling recorded in the destination project. Local CLI paths are rebased relative to that project. A dry run predicts this spelling when it discovers a project root; without an existing repository it reports the expanded invocation source. Dirty dry runs warn about ignored template files on stderr, including schema-only previews and JSON mode.

git tpl --json init emits its outcome on stdout as a single JSON object, with the prose on stderr. The payload is described in JSON output.