# git init: Usage, Options & Common CI Errors

> git init creates a new empty Git repository or reinitializes an existing one. Reference for --bare, --initial-branch, and default-branch-name gotchas.

Source: https://latchkey.dev/learn/command-reference/git-init  
Updated: 2026-06-25

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

| Flag | What it does |
| --- | --- |
| --bare | Create a bare repo (no working tree) for serving |
| --initial-branch=<name> / -b | Set the first branch name (default historically "master") |
| --template=<dir> | Use a custom template directory |
| --quiet / -q | Suppress 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
```

> `git rev-parse --abbrev-ref HEAD` returns the literal string `HEAD` on a detached checkout rather than a branch name. On GitHub Actions read `github.ref_name` instead; the git command cannot know what it was checked out for.

## FAQ

### 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.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
