Creating Your First Repository
A Git repository is a directory that Git is tracking. In this lesson, you'll create your first repository and understand its structure.
What is a Repository?
A repository (or "repo") is simply a folder that Git monitors for changes. It contains:
- Your project files
- A hidden
.gitdirectory with all version history
Creating a New Repository
There are two ways to get a Git repository:
- Initialize a new one in an existing directory
- Clone an existing repository from somewhere else
Let's start by creating a new repository.
Using git init
To turn any directory into a Git repository:
# Navigate to your project folder
cd my-project
# Initialize Git
git init
This creates a .git directory containing:
HEAD- Points to the current branchconfig- Repository configurationobjects/- Stores all content (commits, trees, blobs)refs/- Stores branch and tag references
Starting Fresh
To create a new project with Git:
# Create a new directory
mkdir my-new-project
# Navigate into it
cd my-new-project
# Initialize Git
git init
You'll see:
Initialized empty Git repository in /path/to/my-new-project/.git/
The .git Directory
The .git folder is the heart of your repository. Let's explore its structure:
.git/
├── HEAD # Current branch reference
├── config # Repository configuration
├── description # Used by GitWeb
├── hooks/ # Scripts for Git events
├── info/ # Additional info (like exclude patterns)
├── objects/ # All stored content
└── refs/ # Branch and tag pointers
Important: Never manually edit files in .git unless you know what you're doing!
Checking Repository Status
The git status command shows the current state of your repository:
git status
For a new repository, you'll see:
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
This tells you:
- Which branch you're on (
main) - That there are no commits yet
- That there are no files to track
Creating Your First Files
Let's add some content to track:
# Create a README file
echo "# My Project" > README.md
# Create a source file
echo "console.log('Hello, Git!');" > app.js
Now check the status:
git status
Output:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
app.js
nothing added to commit but untracked files present (use "git add" to track)
The files are "untracked" - Git sees them but isn't tracking their changes yet.
File States in Git
Files in a Git repository can be in one of four states:
Untracked → Tracked
↓ ↓
[Ignored] Unmodified → Modified → Staged
↑ ↓
←←←←←←←←←←←←←←←←←←←←←
(after commit)
| State | Description |
|---|---|
| Untracked | Git doesn't know about this file |
| Unmodified | Tracked file with no changes |
| Modified | Tracked file that has been changed |
| Staged | Changes marked for the next commit |
Adding Files to Tracking
Use git add to start tracking files:
# Add a specific file
git add README.md
# Add multiple files
git add README.md app.js
# Add all files in current directory
git add .
After adding:
git status
Output:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
new file: app.js
The files are now "staged" and ready to be committed.
Making Your First Commit
A commit saves a snapshot of your staged changes:
git commit -m "Initial commit: Add README and app.js"
Output:
[main (root-commit) a1b2c3d] Initial commit: Add README and app.js
2 files changed, 2 insertions(+)
create mode 100644 README.md
create mode 100644 app.js
Now check the status:
git status
Output:
On branch main
nothing to commit, working tree clean
Your first commit is complete!
Viewing Your Commit
See your commit history:
git log
Output:
commit a1b2c3d4e5f6... (HEAD -> main)
Author: Your Name <your.email@example.com>
Date: Mon Jan 15 10:30:00 2025
Initial commit: Add README and app.js
For a compact view:
git log --oneline
Output:
a1b2c3d Initial commit: Add README and app.js
Exercise: Create a Repository
Create a new repository, add a file, and make your first commit:
The Complete Workflow
Here's the basic workflow you'll use repeatedly:
1. Create/Edit files
↓
2. git add <files> (stage changes)
↓
3. git commit -m "msg" (save snapshot)
↓
4. Repeat
Re-initializing a Repository
Running git init in an existing repository is safe - it won't overwrite anything:
# This is safe to run
git init
Output:
Reinitialized existing Git repository in /path/to/project/.git/
Summary
- Use
git initto create a new repository - The
.gitdirectory contains all version history - Use
git statusto check the current state - Files go through states: untracked → staged → committed
- Use
git addto stage files andgit committo save them - Use
git logto view commit history
In the next lesson, we'll explore the basic Git workflow in more detail.
التالي · الدرس 4 من 40
The Basic Git Workflow
قراءة 5 دقيقة
أنهيت 3 من 40 درسًا

اسأل Byte عن هذا الدرس
محادثة خاصة حول هذا الدرس. قرأ Byte الدرس كاملًا، ويجيب خلال ثوانٍ، ويُظهر القسم الذي تأتي منه كل إجابة. يمكنك السؤال بأي لغة.
الأسئلة والأجوبة
أسئلة عامة حول هذا الدرس. يجيب AI Tutor فورًا بلغتك، ويراجع فريق FreeAcademy الإجابات، ويمكن لكل المتعلمين قراءتها.

