Skip to content
LatchkeyLatchkey home

git init: Usage, Options & Common CI Errors

git init turns a directory into a Git repository by creating the .git folder.

Use git init to start version control in a fresh directory or to set up a bare repo for pushing to.

What it does

git init creates a new .git subdirectory containing all the repository metadata. Running it in an existing repo is safe and only reinitializes; it does not overwrite your work.

Common usage

Terminal
git init
git init --initial-branch=main my-project
git init --bare repo.git

Options

FlagWhat it does
--bareCreate a bare repo (no working tree) for serving
--initial-branch=<name> / -bSet the first branch name (default historically "master")
--template=<dir>Use a custom template directory
--quiet / -qSuppress output

Common errors in CI

A common surprise is the default branch name: older Git defaults to "master" while hosts expect "main". Set it explicitly with git init -b main, or configure git config --global init.defaultBranch main so scripts are deterministic across runner images.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

Frequently asked questions

git init: Usage, Options & Common CI Errors?
Use git init to start version control in a fresh directory or to set up a bare repo for pushing to.
What it does?
git init creates a new .git subdirectory containing all the repository metadata. Running it in an existing repo is safe and only reinitializes; it does not overwrite your work.
Common errors in CI?
A common surprise is the default branch name: older Git defaults to "master" while hosts expect "main". Set it explicitly with git init -b main, or configure git config --global init.defaultBranch main so scripts are deterministic across runner images.

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card