git init Command Guide
Create an empty Git repository or reinitialize an existing one
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git initInitialize in current dirgit init <dir>Initialize in new dirgit init --bareCreate bare repositoryOptions
-b <branch>Set initial branch name--template=<dir>Use template directory--sharedSet up for group sharingDownloadable Image Preview
Basic Usage
The git init command creates a new Git repository. It initializes the necessary Git infrastructure in a directory.
# 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 mainWhat git init Creates
Running git init creates a hidden .git directory containing all the necessary Git metadata:
.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 referencesCommon Options
| Option | Description |
|---|---|
| --bare | Create a bare repository (no working directory) |
| -b <name> | Set the initial branch name |
| --template=<dir> | Use a template directory for initial files |
| --shared | Set up repository for group sharing |
Bare Repositories
Bare repositories are used as central repositories (like on GitHub):
# 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 mainPractical Examples
# 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.gitFrequently 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 directorygit init <dir>- Initialize in new directorygit init -b main- Set initial branch namegit init --bare- Create bare repository
Official Documentation
For authoritative information, refer to the official documentation: