DEV Community

ke jia
ke jia

Posted on

Auto git init: The Tiny Feature Every Starter Should Have

Most project scaffolders do one thing: write files. You run the command, you get a directory of source code, and then — and this is the part everyone skips — you remember to git init yourself, add a .gitignore, make the first commit, and set up the remote.

That "then" is where side projects go to die.

I've started treating auto git init as a non-negotiable feature in any scaffolder, and I want to make the case for why a one-line feature is worth more than most of the "fancy" template options.

The death sequence

Watch a side project die:

  1. 9pm, idea strikes. npx <scaffolder>, pick a template, project name, done.
  2. 9:05pm, the app runs. It's working. The dopamine is real.
  3. 9:06pm, "I'll set up git tomorrow." You close the laptop. The idea's momentum is spent.
  4. Tomorrow, you open the project to make one small change. To make the change safely, you need version control. To set up version control, you need to remember the init, the .gitignore, the first commit, the remote, the push. Five steps, each of which is "obvious" and none of which you do, because the project's momentum died with the laptop.
  5. The project becomes a folder you're slightly embarrassed about.

The death wasn't caused by the scaffolder. But the scaffolder could have prevented it by removing the five-step gap between "files exist" and "version control exists."

What auto git init should do

The full feature, in one command, is:

  1. git init in the new project directory.
  2. Write a .gitignore appropriate to the template (Node projects: node_modules/, .env, dist/, *.log).
  3. Exclude .env from the start and include .env.example in the first commit. This is the security-critical bit — the repo's first commit should establish the "env files don't live here" invariant, not leave it to a future .gitignore edit.
  4. Make the first commit. The message is chore: initial scaffold from <template>, and it exists so that the project's history has a clean origin — every future diff is a diff from a known state.

That's it. The scaffolder's output goes from "a directory of files" to "a version-controlled project with a clean first commit and a secrets-safe .gitignore." The user's next action is npm install, not archaeology.

Why the .gitignore-in-the-first-commit matters

Because the alternative is the common sequence: commit everything to get the repo going, then notice .env is tracked, then spend twenty minutes doing git rm --cached .env and rewriting the first commit. The "fix" is itself a source of secrets-in-history incidents — the .env was in commit one for those twenty minutes, and if the repo had a remote (even a private one), it was pushed.

A scaffold that ships with the right .gitignore before the first commit makes the incident impossible by construction. The invariant is in the origin, not in a later patch.

This is the same reason I like pairing scaffolders with secret scanners — dotguard catches the .env-with-real-values case, but the scaffolder is the one that prevents the .env-in-the-repo case from ever existing. Different layers, same goal: the first commit is clean.

The feature in practice

scaffoldx-cli does this as part of its generate step — you run npx scaffoldx-cli, pick a template, name the project, and the output is a git-initialized project with a template-appropriate .gitignore and an initial commit. The whole thing, from empty shell to version-controlled project, is under ten seconds:

$ npx scaffoldx-cli
  [1]  React + Vite + Tailwind    ⚛️
  [2]  Next.js App Router         ▲
  ...
  Choose [1-9]: 1
  Project name: my-app

  ✓ git init + initial commit done
  cd my-app && npm install
Enter fullscreen mode Exit fullscreen mode

The "✓ git init + initial commit done" line is the feature. It's one line of output that removes five steps from the user's path, and it's the difference between "the project exists and is safe to work in" and "the project exists and I need to remember to make it safe."

The meta-point

A scaffolder's job isn't to write the fastest possible template files. It's to get the user from "idea" to "working, version-controlled, safe-to-extend project" in the minimum number of user steps. Every step the user has to remember is a step the project can die at.

Auto git init is the cheapest step-removal in the entire feature list. If your scaffolder doesn't do it, it's leaving the most common death sequence entirely on the user.

npx scaffoldx-cli
Enter fullscreen mode Exit fullscreen mode

More Tools

Tool What it does Command
scaffoldx-cli Production-ready project templates in seconds npx scaffoldx-cli
dotguard Scan .env files for exposed secrets npx @wuchunjie/dotguard
gitpulse Git repo analytics in your terminal npx @wuchunjie/gitpulse
snippetx Terminal code snippet manager npx @wuchunjie/snippetx

If these save you time, consider buying me a coffee. All tools are MIT-licensed, zero-dependency, and run fully offline.

Top comments (0)