Skip to content

init Git Command Guide

The git init command creates an empty Git repository or reinitializes an existing one. It sets up the basic repository structure with .git directory containing objects, refs, and template files, creating an initial branch for development.

Terminal window
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>]
OptionDescription
-q, --quietOnly print error/warning messages
--bareCreate bare repository with no working directory
--template=<dir>Use templates from specified directory
--separate-git-dir <git-dir>Place .git directory at specified location
--object-format=<format>Use SHA-1, SHA-256, etc. for objects
--ref-format=<format>Use reftable or files for refs storage
-b <branch>Set name of initial branch
--initial-branch=<branch>Alternative syntax for initial branch
--shared[=<permissions>]Create group-writable repository
ParameterDescription
<directory>Directory to initialize (default: current)
project/
├── .git/
│ ├── HEAD # Current branch reference
│ ├── config # Repository configuration
│ ├── description # Repository description
│ ├── hooks/ # Git hooks directory
│ ├── info/ # Repository metadata
│ ├── objects/ # Object storage
│ ├── refs/ # Reference storage
│ │ ├── heads/ # Branch references
│ │ └── tags/ # Tag references
│ └── index # Staging area index
└── working-directory/ # Working files
project.git/
├── HEAD # Current branch reference
├── config # Repository configuration
├── description # Repository description
├── hooks/ # Git hooks directory
├── info/ # Repository metadata
├── objects/ # Object storage
└── refs/ # Reference storage
├── heads/ # Branch references
└── tags/ # Tag references
Terminal window
# Initialize in current directory
git init
# Initialize in specific directory
mkdir myproject && cd myproject
git init
# Initialize in existing non-empty directory
cd existing-project
git init
Terminal window
# Create bare repository
git init --bare
# Create named bare repository
git init --bare myrepo.git
# Convert existing to bare
git clone --bare existing-repo bare-repo.git
Terminal window
# Create group-writable repository
git init --shared
# Create with specific permissions
git init --shared=group
git init --shared=world
# Change permissions after creation
git config core.sharedRepository group
chmod -R g+w .git
Terminal window
# Initialize with custom branch name
git init -b develop
# Alternatively
git init --initial-branch=main
# Change branch name later
git branch -m main
Terminal window
# Place .git outside working directory
git init --separate-git-dir=/path/to/git-dir
# Useful for encrypted home directories
git init --separate-git-dir=$HOME/.git/myproject
Terminal window
# Copy .git from existing project
cp -r ../existing-project/.git .
git reset --hard HEAD
# Or re-initialize existing
git init
git remote add origin <url>
git fetch
git checkout -b main origin/main
Terminal window
# Use existing repository as template
git init --template=/path/to/template/repo
# Repository template contains:
# - Hook scripts in hooks/
# - Standard configuration in config
# - Template files like DESCRIPTION, README
Terminal window
# Configure default branch name
git config --global init.defaultBranch main
# Configure default template
git config --global init.templateDir ~/.git-templates
# Configure bare repositories
git config --global init.bare true
# Configure object format
git config --global init.objectFormat sha256
Terminal window
# Initialize repository
git init --initial-branch=main
# Set up basic configuration
git config user.name "Developer Name"
git config user.email "developer@example.com"
# Add initial files
echo "# Project Title" > README.md
git add README.md
git commit -m "Initial commit"
Terminal window
# View repository configuration
git config --list --local
# Set repository-specific options
git config core.repositoryformatversion 1
git config core.filemode true
git config core.ignorecase false
# Configure line endings
git config core.autocrlf input # Linux/Mac
git config core.autocrlf true # Windows
Terminal window
# Check if repository is properly initialized
ls -la .git/
# Check branch status
git branch -v
# Verify repository health
git fsck
# Fix permissions issues
find .git -type f -exec chmod 644 {} \;
find .git -type d -exec chmod 755 {} \;
Terminal window
# Convert format
# Initialize with different object format
git init --object-format=sha256 migrated-repo
cd migrated-repo
git remote add origin ../original-repo
git fetch origin --tags
git branch main origin/main
git checkout main
# Migrate to reftable format
git init --ref-format=reftable new-repo
Terminal window
mkdir web-project && cd web-project
git init --initial-branch=main
# Create basic structure
mkdir public css js views
touch index.html style.css app.js
# Add gitignore
cat > .gitignore << 'EOF'
node_modules/
dist/
*.log
EOF
git add .
git commit -m "Initial web project structure"
Terminal window
mkdir mylib && cd mylib
git init --bare
# Or non-bare for development
git init --initial-branch=develop
# Set up branch structure
git config core.abbrev 8
touch README.md lib.py tests/
git add .
git commit -m "Library skeleton"
Terminal window
# Initialize with custom settings
git init --template=~/.git-templates/research --initial-branch=main
# Configure for large files
git config core.bigFileThreshold 50m
git config pack.windowMemory 256m
# Set up hooks for data validation
cp ~/.git-templates/research/hooks/post-commit .git/hooks/
chmod +x .git/hooks/post-commit

Why doesn’t git init overwrite existing repositories?

Section titled “Why doesn’t git init overwrite existing repositories?”

git init is designed to be idempotent. It only creates missing structure, never destroys existing data. Running multiple times is safe.

What’s the difference between —bare and regular init?

Section titled “What’s the difference between —bare and regular init?”

Bare repositories have no working directory, contain only Git data. Regular repositories have both .git metadata and working files. Use bare for remotes.

How do I change the initial branch after creation?

Section titled “How do I change the initial branch after creation?”

Use git branch -m to rename the default branch. Set init.defaultBranch config to change default for future repositories.

Can I initialize a repository in a subdirectory?

Section titled “Can I initialize a repository in a subdirectory?”

Yes, specify the directory name as parameter. git init creates and initializes the specified directory. The .git folder goes inside it.

What’s the purpose of repository templates?

Section titled “What’s the purpose of repository templates?”

Templates provide boilerplate hooks, configurations, and standard files for consistent repository setup across projects.

—shared option makes repository group-writable, allowing multiple users to collaborate directly without needing personal clones.

What’s the impact of object format choice?

Section titled “What’s the impact of object format choice?”

SHA-256 provides better security than SHA-1, but requires Git 2.29+. SHA-1 remains default for compatibility. All clones must support chosen format.

Can I convert existing repositories to different formats?

Section titled “Can I convert existing repositories to different formats?”

Yes, but requires Git migration workflows. recreating repository with desired format and carefully migrating history.

How do I handle global vs local configurations?

Section titled “How do I handle global vs local configurations?”

Global settings apply to all repositories, local only to specific repo. Use appropriate config command for desired scope (—global, —local, or —system).

What’s the safest way to redo repository initialization?

Section titled “What’s the safest way to redo repository initialization?”

Avoid reinit on working repos. Instead, manually clean up .git directory and reinit if necessary. Always backup important work first.

cat .git/config shows format settings, ls -la .git shows directory structure, git version shows Git capabilities.

Possible but performance suffers due to network latency. Local storage preferred, especially for object database access.

  1. Project Initialization: Create repository for new software projects with proper branch and configuration setup
  2. Migration Workflows: Convert existing projects to Git version control with custom settings
  3. Server Repository Creation: Set up bare repositories for team access and deployment pipelines
  4. Template-Based Setup: Rapid repository initialization using predefined configurations and hooks
  5. Educational Git Exploration: Safe repository creation for learning Git commands and workflows
  6. Automated Repository Management: Programmatic repository creation in continuous integration systems