Creating Your First Git Repository
Now that you have Git installed and configured, it's time to create your first repository! In this lesson, you'll learn how to initialize a Git repository and understand what happens behind the scenes.
Learning Goals:
- Initialize a new Git repository using
git init - Understand the structure of the
.gitdirectory - Create your first repository from scratch
- Verify repository initialization
Initializing a Git Repository
To start tracking your project with Git, you need to create a repository. This is done using the git init command.
mkdir my-first-project
cd my-first-project
git init
After running these commands, you should see output similar to:
Initialized empty Git repository in /path/to/your/my-first-project/.git/
Always run git init from within your project directory. If you're starting a new project, create the directory first, then navigate into it before initializing Git.
Understanding the .git Directory
When you initialize a Git repository, Git creates a hidden .git directory in your project root. This directory contains all the metadata and object database for your repository.
ls -la
You should see output showing the .git folder:
drwxr-xr-x 3 user staff 96 Jan 15 10:30 .
drwxr-xr-x 15 user staff 480 Jan 15 10:30 ..
drwxr-xr-x 10 user staff 320 Jan 15 10:30 .git
Let's explore what's inside:
ls -la .git/
The key components you'll see include:
HEAD- points to the current branchconfig- repository-specific configurationobjects/- stores all repository contentrefs/- stores pointers to commits (branches and tags)
Don't manually modify files in the .git directory unless you know exactly what you're doing. Corruption of these files can make your repository unusable.
Creating Your First Project
Let's create a simple project structure to practice with:
echo "# My First Project" > README.md
mkdir src
echo "console.log('Hello, Git!');" > src/main.js
Now your project structure should look like:
my-first-project/
.git/
README.md
src/
main.js
Verifying Repository Status
You can check the status of your repository at any time using git status:
git status
You should see output indicating that you have untracked files:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
src/
nothing added to commit but untracked files present (use "git add" to track)
Repository Configuration Check
Let's verify our repository is properly configured:
git config --list
This shows both global and repository-specific configuration. You should see your name and email from the previous lesson, plus repository-specific settings.
Common Pitfalls
- Initializing in the wrong directory: Make sure you're in your project root before running
git init - Nested repositories: Avoid running
git initinside an existing repository, as this creates a submodule - Case sensitivity: Git is case-sensitive on most systems -
MyFile.jsandmyfile.jsare different files - Permission issues: Ensure you have write permissions in the directory where you're initializing the repository
- Forgetting to check status: Always use
git statusto verify your repository state before proceeding
Summary
In this lesson, you successfully:
- Created a new Git repository using
git init - Understood the purpose of the
.gitdirectory - Created a simple project structure
- Verified your repository status and configuration
Your repository is now ready for tracking changes! In the next lesson, you'll learn how to add files to staging and make your first commit.
Quiz
Initializing a Git Repository – Quick Check
What command initializes a new Git repository?