Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Setup and Basics

git init command

Reference for git init: create a new repository, choose the initial branch, make bare repositories, set the object format and re-initialize safely.

git init turns a directory into a Git repository. It creates the hidden .git directory with an empty object database, a HEAD that points at an unborn branch, and a default config. Nothing is committed yet, and no files are tracked until you stage them.

Running git init in a directory that already is a repository is safe: it never touches your commits or branches. It only refreshes templates and re-reads some settings, which is why the command prints "Reinitialized existing Git repository".

Terminal
git init [-q] [-b <branch>] [--bare] [--template=<dir>]
         [--separate-git-dir=<dir>] [--object-format=<format>] [<directory>]

Try it

Bash Create a repository and look inside

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Initial branch, bare repositories and the object format

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
git init [<directory>]
git init --bare [<directory>]
git init -b <name> [<directory>]
Name Description
<directory> Where to create the repository. Missing directories (including parents) are created. Without it, the current directory is used.
-q, --quiet Print only errors and warnings.
-b <name>, --initial-branch=<name> Name of the first branch. Without it Git uses init.defaultBranch, and falls back to master if that is unset.
--bare Create a repository with no working tree. The files that normally live in .git are placed directly in the directory.
--object-format=<format> sha1 (default) or sha256. Chosen once, for the life of the repository.
--ref-format=<format> Reference storage: files (default) or reftable, if your build supports it.
--template=<dir> Copy files from this directory into .git instead of the built-in templates (sample hooks, info/exclude, description).
--separate-git-dir=<dir> Store the repository in <dir> and leave a .git file in the working tree that points there.
--shared[=<perm>] Make the repository group-writable (group), world-readable (all/world), or use an octal mode. Used for servers shared by several accounts.

Exit status: 0 on success, nonzero if the directory cannot be created or written to.

Edge cases: if <directory> is an existing file, the command fails. Passing --bare and --separate-git-dir together is an error. An already-initialized repository keeps its object format; asking for a different one on re-init fails.

Usage notes

  • Branch name. The first branch is created lazily with the first commit, but HEAD already names it. Set a permanent preference with git config --global init.defaultBranch main, see git config.
  • Bare repositories are what servers hold: they have no checked-out files, so nobody can push into a branch that is "in use". Their names conventionally end in .git. Create one for a central remote, then git clone it.
  • SHA-256 repositories cannot exchange objects with SHA-1 repositories, and some hosting services do not accept them yet. Treat the choice as an experiment unless your whole toolchain supports it.
  • Templates. The sample hooks in .git/hooks/*.sample come from the template directory. Set init.templateDir to give every new repository your own hooks, exclude patterns or description.
  • Safe to repeat. Running git init again does not erase anything. It is also how you pick up a changed template.
  • Not the same as cloning. git init creates an empty history; to start from an existing project use git clone.
  • Nested repositories. Running git init inside another repository's working tree creates a second, independent repository there. The outer one then sees it as an embedded repository, which usually is a mistake (see submodules).
  • Environment. GIT_DIR and GIT_WORK_TREE change where the repository is created.