The Hook (The "Byte-Sized" Intro)
Any folder can become a Git repository. One command — git init — and suddenly your folder has a photographic memory. It remembers every file you add, every change you make, and every mistake you fix. Let's give your first project a memory.
📖 What is Creating Your First Repository?
A repository (repo) is a project folder tracked by Git. Creating one involves initializing Git in a directory, adding files, and making your first commit. This is the "Hello World" of Git — once you do it, the entire workflow clicks.
Conceptual Clarity
git initcreates a hidden.git/directory inside your project folder. This is the repository — it stores all history, configuration, and metadata.git addmoves files from the working tree to the staging area — selecting what goes into the next snapshot.git committakes everything in the staging area and saves it as a permanent snapshot with a timestamp, author, and message.git logshows the timeline of all commits.- You can also clone an existing repo instead of initializing a new one.
Real-Life Analogy
Think of it like starting a new journal:
- Buy a notebook =
git init(create the repository) - Write on a page = edit files (working tree)
- Mark the page as ready =
git add(staging) - Date and sign the entry =
git commit(permanent snapshot) - Flip back through pages =
git log(history)
Visual Architecture
Why It Matters
- Without
git init, Git commands won't work in your folder — it's just a normal directory. - Your first commit is the root of your entire project history — every future commit chains back to it.
- Understanding this flow (init → add → commit) is the foundation for everything else in Git.
Code
# ─── Step 1: Create a project folder ───
mkdir my-first-repo
cd my-first-repo
# ─── Step 2: Initialize Git ───
git init
# Output: Initialized empty Git repository in /path/to/my-first-repo/.git/
# ─── Step 3: Verify — the .git folder exists ───
ls -la .git/
# Output: HEAD config hooks/ objects/ refs/ ...
# ─── Step 4: Create your first file ───
echo "# My First Repo" > README.md
# ─── Step 5: Check status — Git sees the new file ───
git status
# Output:
# Untracked files:
# README.md
# ─── Step 6: Stage the file ───
git add README.md
# ─── Step 7: Check status again — file is staged ───
git status
# Output:
# Changes to be committed:
# new file: README.md
# ─── Step 8: Commit with a message ───
git commit -m "Initial commit: add README"
# Output:
# [main (root-commit) a1b2c3d] Initial commit: add README
# 1 file changed, 1 insertion(+)
# create mode 100644 README.md
# ─── Step 9: View your history ───
git log --oneline
# Output:
# a1b2c3d (HEAD -> main) Initial commit: add README
# ─── Step 10: See what's inside the commit ───
git show
# Output: Shows the diff of your first commitgit init vs git clone
| Scenario | Command | What Happens |
|---|---|---|
| Start a new project from scratch | git init | Creates an empty repo with no commits |
| Get an existing project from GitHub | git clone <url> | Downloads the full repo + all history |
# Clone an existing project
git clone https://github.com/user/project.git
cd project
git log --oneline -3
# Output: Shows the last 3 commits from the remoteKey Takeaways
git initcreates a repository — it adds a.git/folder to track your project.- The flow is create → add → commit — this is the core loop you'll use thousands of times.
- Your first commit is the
(root-commit)— the foundation of your entire project history. - Use
git cloneinstead ofgit initwhen you're joining an existing project.
Interview Prep
-
Q: What does
git initdo? A: It creates a.git/subdirectory in the current folder, initializing it as a Git repository. This directory contains all the objects, refs, HEAD pointer, and configuration that Git needs to track changes. -
Q: What is the difference between
git initandgit clone? A:git initcreates a new, empty repository.git clonecopies an existing remote repository (including all files, branches, and history) to your local machine. Clone also automatically sets up the remote asorigin. -
Q: What does a
root-commitmean in Git? A: A root commit is the very first commit in a repository. It has no parent commit. All subsequent commits form a chain (directed acyclic graph) starting from this root.