Create Your First Local Repo
Create your first local repository with Git. Learn the core concept, step-by-step commands, troubleshooting tips, and what to study next in this Git Tutorial lesson.
Focus: create your first local repository
You’ve just written a brilliant piece of code — maybe a Python script, a config file, or the start of a project. But right now, that work is just an anonymous file on your disk: no history, no safety net, no way to roll back a bad edit. This is exactly the pain git init exists to solve. When you create your first local repository, you’re not just making a folder — you’re giving your project a memory, a timeline, and the power to experiment without fear. In this lesson, you’ll transform a plain directory into a Git repository, verify it works, and take the first step toward mastering version control.
The Problem This Lesson Solves
Without version control, every file you save is a gamble. You might find yourself manually copying files to project_final_v2_actually_final.py — a habit that breeds chaos and loss. A single bad save, an accidental deletion, or a half-finished refactor can destroy hours of work with no way back.
Git solves this by tracking every change you make. But before Git can track anything, it needs a repository — a hidden storehouse of metadata where your project’s history will live. Creating that storehouse is the very first step in any Git workflow, and it’s the foundation for everything else: committing, branching, merging, and collaborating.
This lesson is your first practical step: you’ll take an ordinary project folder and turn it into a Git repository. By the end, you’ll know not just how to do it, but why it matters — and you’ll be ready to make your first commit.
Core Concept / Mental Model
Think of a Git repository as a time machine for your project. The time machine doesn’t change your files — it observes them, records snapshots (commits), and lets you jump back and forth. The machine’s control panel lives inside your project folder, in a hidden directory named .git.
When you run git init, you’re essentially powering on that time machine. Git creates the .git directory, which contains:
- Object database — stores all historical snapshots of your files.
- Index (staging area) — a preview of changes you’re about to commit.
- HEAD pointer — tells Git which commit you’re currently looking at.
Key definitions: - Working tree: the files and folders you actually see and edit. - Index (staging area): a middle ground where you prepare changes before committing. - Commit: a permanent snapshot of your project at a moment in time.
Pro tip: A repository is local first. Everything — history, branches, data — lives on your machine until you explicitly push to a remote (like GitHub). That means
git initworks even offline, and nothing is shared unless you say so.
This mental model will guide you through every future Git command. Remember: the repository is the brain; your working files are the body. Git wires them together through the index.
How It Works Step by Step
Creating a repository is a two-step process:
- Navigate to your project directory (or create one).
- Run
git initto start tracking it.
That’s the core. But to make it truly useful, you’ll typically add files, stage them, and make your first commit.
Here’s the logical flow:
- Step 1: Prepare your project — Create a folder and a file inside it. This gives Git something to track.
- Step 2: Initialize the repository —
git initcreates the.gitdirectory. Your folder is now a repository, but Git doesn’t know about your files yet. - Step 3: Check status —
git statusshows what Git sees: untracked files, staged changes, current branch. - Step 4: Stage files —
git addtells Git “I want these changes in the next snapshot.” - Step 5: Commit —
git commit -m "message"actually creates the snapshot in history.
Understanding cause and effect here is crucial:
- Cause: You edit a file.
- Effect: Git sees the change when you run git status.
- Cause: You stage the change with git add.
- Effect: The change is queued in the index.
- Cause: You commit.
- Effect: A permanent, recoverable snapshot is stored in history.
This sequence — init, add, commit — will become second nature. The key is that git init only happens once per project (unless you’re reinitializing). After that, your entire workflow revolves around staging and committing changes.
Hands-On Walkthrough
Let’s put theory into practice. Open a terminal and follow along. I’ll assume you’re using a Unix-like shell (macOS/Linux) or Git Bash on Windows.
1. Create a project directory
mkdir my-first-repo
cd my-first-repo
2. Add your first file
echo "# My First Repo" > README.md
Now you have a folder with a single file. Nothing special yet.
3. Initialize the repository
git init
Expected output:
Initialized empty Git repository in /path/to/my-first-repo/.git/
That’s it — you’ve created your first local repository! But you’re not done yet. Let’s verify and commit.
4. Check the status
git status
Expected output (paraphrased):
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
Notice Git says “No commits yet” — the repository exists, but there’s no history. Also, README.md is listed as untracked, meaning Git isn’t watching it yet.
5. Stage and commit
git add README.md
git commit -m "Initial commit"
Output will show something like:
[master (root-commit) 1a2b3c4] Initial commit
1 file changed, 1 insertion(+)
create mode 100644 README.md
You now have your first commit! Let’s verify:
git log --oneline
Output:
1a2b3c4 (HEAD -> master) Initial commit
Pro tip: If your default branch is
maininstead ofmaster(newer Git versions), don’t worry — both are valid. You can rename later withgit branch -m main.
Full script (copy-paste friendly)
# One-liner to get from zero to first commit
mkdir my-first-repo && cd my-first-repo
echo "# My First Repo" > README.md
git init
git add README.md
git commit -m "Initial commit"
Expected final result: git log shows one commit, and git status says your working tree is clean. You’ve successfully created your first local repository and made it meaningful with a commit.
Compare Options / When to Choose What
When creating a repository, you have a few choices:
| Option | Command | Use case |
|---|---|---|
| Local only | git init |
Personal projects, experimenting, no need to share yet |
| Clone existing | git clone <url> |
Working on a project that already exists on a remote (e.g., GitHub) |
| Initialize with remote | git init + git remote add origin <url> |
You already have a project locally but want to sync it to a remote later |
# Example: add a remote after local init
git remote add origin https://github.com/username/my-first-repo.git
git push -u origin master
- Choose
git initwhen starting from scratch — you’re the creator. - Choose
git clonewhen you’re joining an existing project — the repo (and history) already exists elsewhere. - Choose init + remote when you’ve been working locally and want to back up or share later.
Variations:
- Use git init -b main to set the default branch to main immediately.
- Use git init --bare to create a repository with no working tree, typically for a central server (advanced).
- Use a GUI tool like VS Code’s “Initialize Repository” button — it runs git init under the hood.
Troubleshooting & Edge Cases
Let’s address the common stumbling blocks you might hit.
“Not a git repository” error
If you run git status and get fatal: not a git repository, you’re either in the wrong directory or you forgot to run git init. Fix: cd into your project folder and run git init.
Accidentally initialized in the wrong folder
You ran git init in your home directory? The .git folder is now there. Fix: Delete the .git folder (rm -rf .git) to untrack everything. It’s safe if you haven’t committed anything you care about.
“Default branch name” warnings
Git may warn: hint: Using 'master' as the name for the initial branch. This is harmless. To avoid it, use git init -b main. To change your global default: git config --global init.defaultBranch main.
Commit failed — “Please tell me who you are”
This happens when Git doesn’t know your identity. Fix: set it globally:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Untracked files but you want to include them
If git status shows untracked files, you must git add them. If you forgot to include a file in your commit, just git add it and commit again. You can also use git add -A to stage everything.
“Everything up-to-date” when you expect changes
If git push says “Everything up-to-date,” you may have forgotten to commit after staging. Remember: staging ≠ committed. Always run git commit after git add.
Accidental .git deletion
If you delete .git, you lose all history (but your working files remain). You can re-init, but you’ll start a fresh history. Always back up your repository if you care about history.
What You Learned & What’s Next
You now know how to create your first local repository — the foundation of all Git workflows. You can:
- Run git init to turn any folder into a repository.
- Check the state of your project with git status.
- Stage and commit changes to create a permanent snapshot.
- Recognize and fix common rookie errors like wrong directory or missing user config.
You achieved the lesson objectives: explaining the core idea behind local repositories and completing a hands-on exercise to create one.
Now you’re ready to take your first commit to the next level. In the next lesson, you’ll learn how to make a meaningful first commit — writing good commit messages, staging selectively, and understanding how to build a clean history. That’s where the real power of Git begins.
Pro tip: Practice recreating a repository from scratch a few times. Muscle memory will save you when you’re working on real projects under pressure.
Practice recap
Create a new folder called practice-git, add a hello.txt file with your name in it, then run git init, git add hello.txt, and git commit -m 'Add hello.txt'. Then attempt to commit a second change and observe how Git tracks it. This mirrors exactly the workflow you'll use on every project.
Common mistakes
- Running
git initin the wrong directory — always double-checkpwdfirst. - Forgetting to commit after staging — files stay in the index until you run
git commit. - Deleting the
.gitfolder thinking it's unnecessary — that erases all history. - Using
git initinstead ofgit clonewhen joining an existing project — you lose the remote history.
Variations
- Use
git init -b mainto set the initial branch tomaininstead ofmaster. - Use
git init --barefor a server-side repository with no working files (for shared central repos). - Use GUI tools like VS Code's 'Initialize Repository' button — they call
git initunder the hood.
Real-world use cases
- Setting up version control for a new Python project on your local machine before pushing to GitHub.
- Creating a personal notes or config repo to track changes to dotfiles across machines.
- Initializing a repo for a small team project on a shared server without a central hub (e.g., GitLab self-hosted).
Key takeaways
git initcreates a local repository and a hidden.gitdirectory — the command only needs to be run once per project.git statusis your best friend for understanding what Git sees: untracked, staged, or committed.- A file isn't part of history until you stage it (
git add) and commit it (git commit -m 'message'). - Your Git identity (
user.nameanduser.email) must be set before your first commit. - The default branch name may be
masterormaindepending on config — both are valid and can be renamed. - Deleting
.gitwipes all history; always think twice before doing that.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.