Lesson Completion
Back to course

Creating Your First Repository

Beginner
12 minutes4.9Git

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 init creates a hidden .git/ directory inside your project folder. This is the repository — it stores all history, configuration, and metadata.
  • git add moves files from the working tree to the staging area — selecting what goes into the next snapshot.
  • git commit takes everything in the staging area and saves it as a permanent snapshot with a timestamp, author, and message.
  • git log shows 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:

  1. Buy a notebook = git init (create the repository)
  2. Write on a page = edit files (working tree)
  3. Mark the page as ready = git add (staging)
  4. Date and sign the entry = git commit (permanent snapshot)
  5. Flip back through pages = git log (history)

Visual Architecture

flowchart LR A["📁 Empty Folder"] -->|"git init"| B["📁 Git Repo<br/>(.git/ created)"] B -->|"create files"| C["📝 Working Tree"] C -->|"git add"| D["📋 Staging Area"] D -->|"git commit"| E["💾 First Commit"] E -->|"git log"| F["📚 History"] style A fill:#1a1a2e,stroke:#e94560,color:#e94560 style B fill:#1a1a2e,stroke:#e94560,color:#e94560 style C fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style D fill:#0f3460,stroke:#53d8fb,color:#53d8fb style E fill:#0f3460,stroke:#53d8fb,color:#53d8fb style F fill:#0f3460,stroke:#53d8fb,color:#53d8fb

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

bash
# ─── 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 commit

git init vs git clone

ScenarioCommandWhat Happens
Start a new project from scratchgit initCreates an empty repo with no commits
Get an existing project from GitHubgit clone <url>Downloads the full repo + all history
bash
# 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 remote

Key Takeaways

  • git init creates 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 clone instead of git init when you're joining an existing project.

Interview Prep

  • Q: What does git init do? 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 init and git clone? A: git init creates a new, empty repository. git clone copies an existing remote repository (including all files, branches, and history) to your local machine. Clone also automatically sets up the remote as origin.

  • Q: What does a root-commit mean 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.

Topics Covered

Git FundamentalsGit Introduction

Tags

#git#init#commit#repository

Last Updated

2026-02-12