Terminal GuideTerminal Guide

git init Command Guide

Create an empty Git repository or reinitialize an existing one

4 min readLast updated: January 19, 2026
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

git initInitialize in current dir
git init <dir>Initialize in new dir
git init --bareCreate bare repository

Options

-b <branch>Set initial branch name
--template=<dir>Use template directory
--sharedSet up for group sharing

Downloadable Image Preview

Failed to generate preview

Basic Usage

The git init command creates a new Git repository. It initializes the necessary Git infrastructure in a directory.

bash
# Initialize Git in current directory
git init

# Initialize Git in a new directory
git init my-project

# Initialize with a specific branch name
git init -b main

What git init Creates

Running git init creates a hidden .git directory containing all the necessary Git metadata:

bash
.git/
├── HEAD              # Points to current branch
├── config            # Repository configuration
├── description       # Repository description
├── hooks/            # Git hooks scripts
├── info/             # Exclude patterns
├── objects/          # Object database
└── refs/             # Branch and tag references

Common Options

OptionDescription
--bareCreate a bare repository (no working directory)
-b <name>Set the initial branch name
--template=<dir>Use a template directory for initial files
--sharedSet up repository for group sharing

Bare Repositories

Bare repositories are used as central repositories (like on GitHub):

bash
# Create a bare repository
git init --bare my-project.git

# Clone from a bare repository
git clone /path/to/my-project.git

# Push to a bare repository
git push origin main

Practical Examples

bash
# Start a new project
mkdir my-project
cd my-project
git init
echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"

# Initialize with main as default branch
git init -b main my-project

# Initialize and connect to remote
git init
git remote add origin git@github.com:user/repo.git

# Reinitialize existing repo (safe operation)
git init

# Create shared repository on server
git init --bare --shared /srv/git/project.git

Frequently Asked Questions

What is the difference between git init and git clone?

"git init" creates a new empty repository, while "git clone" copies an existing repository. Use init for new projects and clone for existing ones.

Can I run git init in an existing project?

Yes! Running git init in a directory with existing files is safe. It creates the .git directory without affecting your files. You can then add and commit the existing files.

What is a bare repository?

A bare repository has no working directory - only the Git database. It's used as a central repository that developers push to and pull from, typically on a server.

How do I change the default branch name?

Use "git init -b main" to set the initial branch name, or configure it globally with "git config --global init.defaultBranch main".

Can I undo git init?

Yes, simply delete the .git directory: "rm -rf .git". This removes all Git tracking while keeping your files intact.

Summary

git init is the first step in creating a Git repository. Use it to start tracking a new or existing project with version control.

Quick Reference

  • git init - Initialize in current directory
  • git init <dir> - Initialize in new directory
  • git init -b main - Set initial branch name
  • git init --bare - Create bare repository

Official Documentation

For authoritative information, refer to the official documentation:

Related Articles