Git by Example: Repository Initialization
Learn how to start a new Git repository using the git init command. This example explains what happens under the hood when you initialize a project and how to prepare your directory for version control.
Code
# Navigate to your project directory
cd /path/to/your/project
# Initialize a new Git repository
git init
# Output:
# Initialized empty Git repository in /path/to/your/project/.git/
# Check the status of your new repository
git status
# Output:
# On branch master
#
# No commits yet
#
# nothing to commit (create/copy files and use "git add" to track)Explanation
Initializing a Git repository is the first step in tracking your project's history. The git init command transforms your current directory into a Git repository by creating a hidden .git subdirectory. This directory houses all the metadata and object database that Git uses to manage version control.
When you run this command, Git sets up the necessary internal structures but does not automatically track existing files. You start with an empty staging area, and you must explicitly add files using git add before they can be committed. This gives you control over exactly what gets included in your project's history.
This command can be used to start a fresh project or to convert an existing, unversioned project into a Git repository. It is a local operation that prepares your workspace for all subsequent Git commands.
- Creates a
.gitdirectory for metadata - Sets up the default branch (usually master or main)
- Does not modify your project files
- Prepares the directory for
git addandgit commit

