Skip to main content

Synopsis

git init [-q | --quiet] [--bare] [--template=<template-directory>]
         [--separate-git-dir <git-dir>] [--object-format=<format>]
         [--ref-format=<format>]
         [-b <branch-name> | --initial-branch=<branch-name>]
         [--shared[=<permissions>]] [<directory>]

Description

The git init command creates an empty Git repository - basically a .git directory with subdirectories for objects, refs/heads, refs/tags, and template files. An initial branch without any commits will be created.
Running git init in an existing repository is safe. It will not overwrite things that are already there.

Common Usage

1

Create a new repository

Initialize a new Git repository in the current directory:
git init
This creates a .git subdirectory in the current directory.
2

Create a repository in a specific directory

Initialize a repository in a new directory:
git init my-project
cd my-project
3

Create a bare repository

For server-side repositories, create a bare repository:
git init --bare /path/to/repo.git

Options

Only print error and warning messages; all other output will be suppressed.
git init --quiet
Create a bare repository. If GIT_DIR environment is not set, it is set to the current working directory.
git init --bare repo.git
Bare repositories are typically used as centralized repositories on servers.
Specify the object format (hash algorithm) for the repository. Valid values are sha1 and sha256. Default is sha1.
git init --object-format=sha256
Specify the reference storage format for the repository.
git init --ref-format=reftable
Use the specified name for the initial branch in the newly created repository.
git init -b main
The default branch name can be customized via the init.defaultBranch configuration variable.
Specify the directory from which templates will be used.
git init --template=/path/to/templates
Instead of initializing the repository in ./.git/, create a text file there containing the path to the actual repository.
git init --separate-git-dir=/path/to/git-dir
Specify that the Git repository is to be shared amongst several users.
git init --shared=group
Valid values:
  • false or umask: Use permissions reported by umask(2)
  • true or group: Make repository readable and writable by group
  • all or world or everybody: Make repository readable by all users
  • 0xxx: Octal permissions

Examples

Create a new project

# Create and initialize a new project
mkdir my-app
cd my-app
git init

# Create initial files
echo "# My App" > README.md
git add README.md
git commit -m "Initial commit"

Initialize with custom branch name

# Create repository with 'main' as default branch
git init -b main my-project

Create a shared repository

# Create a shared repository accessible by group members
git init --shared=group --bare /srv/git/project.git
  • git clone - Clone a repository into a new directory
  • git add - Add file contents to the index
  • git commit - Record changes to the repository

Build docs developers (and LLMs) love