Git Notes
Learn git init command to create new repositories, understand .git directory structure, initialize existing projects, and set up repository configuration for version control.
git init creates a new Git repository in your project folder. This is the first command you run when starting a new project with Git.
Basic Usage
# Create repository in current directory
git init
# Output:
# Initialized empty Git repository in /path/to/project/.git/What Gets Created
# Check what was created
ls -la
# Output:
# total 8
# drwxr-xr-x 3 user group 96 Jun 13 14:00 .
# drwxr-xr-x 8 user group 256 Jun 13 14:00 ..
# drwxr-xr-x 9 user group 288 Jun 13 14:00 .git
# Inside .git (Git's internal folder)
ls -la .git
# Output:
# HEAD (points to current branch)
# config (repository settings)
# description (repository description)
# hooks/ (scripts that run on Git events)
# objects/ (compressed commits/blobs)
# refs/ (branches and tags)Initialize with Specific Branch
# Modern repositories use 'main' not 'master'
git init -b main
# Or after init
git config --global init.defaultBranch mainInitialize in Existing Folder
# If folder doesn't exist
mkdir my-project
cd my-project
git init
# If folder exists with files
cd existing-project
git init # Safe! Doesn't modify existing filesInitialize and Connect to GitHub
# Create locally
git init my-project
cd my-project
# Make first commit
echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"
# On GitHub, create empty repository
# Connect to GitHub
git remote add origin https://github.com/username/my-project.git
git branch -M main
git push -u origin mainThe .git Directory (Don't Touch!)
Never manually edit .git folder! Git manages it automatically.
Verify Repository Created
git status
# Output:
# On branch main
# No commits yet
# nothing to commit (create/modify files and use "git add" to track)Creating Multiple Repositories
# Project 1
mkdir project1
cd project1
git init
# Project 2
cd ..
mkdir project2
cd project2
git init
# Each is independent
# Each has own .git folder
# No connection between themDelete Repository
# Remove Git tracking (keep files)
rm -rf .git
# Now it's no longer a Git repository
git status
# Output: fatal: not a git repositoryCommon Use Cases
New Project from Scratch
mkdir new-project
cd new-project
git init
echo "# New Project" > README.md
git add README.md
git commit -m "Initial commit"Existing Project
cd existing-project
git init
git add .
git commit -m "Initial commit - add existing files"Project Template
# Create template repository
git init my-template
cd my-template
# Add boilerplate files, configuration, etc.
git add .
git commit -m "Template setup"
# Use as template
git clone my-template new-project
cd new-project
git remote rm origin # Disconnect from templateInterview Q&A
Q1: What does git init do?
A: git init creates a new Git repository by creating a hidden .git folder containing all Git's internal data. This enables version control for the project. Must be run once per project before using other Git commands. Safe to run on existing files - doesn't modify them.
Q2: What's inside the .git folder?
A: .git contains Git's internal database: objects/ stores compressed commits, refs/ stores branch pointers, HEAD points to current branch, config stores repository settings, hooks/ contains scripts for Git events. This folder is automatically managed by Git - never manually edit it.
Q3: Difference between git init and git clone?
A: git init creates new empty repository locally. git clone copies existing repository from remote (like GitHub). Use init for new projects, clone to get copy of existing project. After init, you start from scratch. After clone, you have complete project history.
Q4: How do you initialize with 'main' branch instead of 'master'?
A: Run git init -b main to create repository with main branch as default. Or configure globally: git config --global init.defaultBranch main. Modern projects use main instead of outdated master term.
Q5: Can you safely run git init on folder with existing files?
A: Yes, completely safe. git init only creates .git folder. Doesn't delete, modify, or move existing files. This is how you add Git to existing projects. After init, stage and commit your existing files to start tracking them.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Init - Initialize Repository.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, basics, init, git init - initialize repository
Related Git & GitHub Topics