35 Mins

3. Your First Repository

Core Concepts

A repository (repo) is a directory that Git is tracking. It contains your files plus a hidden .git folder that stores the entire history. You never touch the .git folder directly — everything goes through Git commands.

Creating a New Repository

Create a new project folder and initialise Git inside it:

Bash
mkdir my-project
cd my-project
git init

See the hidden .git folder Git created. You'll see a .git directory — that IS the repository:

Bash
ls -la

The Three States of a File

Every file in a Git repository is always in one of three states. Understanding this is the foundation of all Git operations:

Working Directory

Files you are actively editing. Changes are not yet tracked by Git. This is your normal file system.

Staging Area (Index)

Files you have marked as ready to be committed. git add moves files here. Think of it as a loading dock.

Repository (History)

Committed files are permanently saved in Git history. git commit moves staged files here.

The Core Workflow: Modify → Stage → Commit

Step 1: Check the current state of your repository. This shows untracked files, modified files, and staged files.

Bash
git status

Step 2: Create a file.

Bash
echo "# My Project" > README.md

Step 3: Check status again. README.md is now untracked.

Bash
git status

Step 4: Stage the file. Add it to the staging area. To stage ALL changed files at once, use git add .

Bash
git add README.md
git add .

Step 5: Commit the staged changes with a message.

Bash
git commit -m "Add project README"

Step 6: View the history. This shows each commit: hash, author, date, and message.

Bash
git log

Writing Good Commit Messages

Commit messages are the only documentation of why a change was made. In six months, when something breaks, your commit history is the investigation tool. Write messages for your future self:

  • Bad: fix stuff
  • Bad: updated files
  • Bad: wip
  • Good: Fix null pointer exception when user profile is empty
  • Good: Add rate limiting to /api/login endpoint (prevents brute force)
  • Good: Refactor database connection pool — reduce timeout from 30s to 5s
The Conventional Commits standard: Many teams prefix commits with a type: feat: for new features, fix: for bug fixes, docs: for documentation, refactor: for code cleanup. This enables automated changelog generation.

Creating a Repository on GitHub

Most projects start on GitHub first, then get cloned locally. Here is the exact sequence every professional uses:

  1. Go to github.com → click the + icon → New repository
  2. Give the repo a name (e.g., my-project). Use lowercase and hyphens — no spaces.
  3. Choose Public (visible to anyone) or Private (only you + collaborators).
  4. Check Add a README file so the repo is not empty.
  5. Click Create repository.

GitHub now shows you the repo. To bring it to your local machine:

Bash
git clone git@github.com:yourusername/my-project.git
cd my-project

You are now inside a fully wired Git repository — origin already points to GitHub. Start coding immediately.

Starting a Local Project and Pushing to GitHub

Sometimes you start coding locally before creating a GitHub repo. Here is how you wire them together:

Bash
mkdir my-project
cd my-project
git init
git add .
git commit -m "feat: initial project setup"

Now create a blank repo on GitHub (do NOT check README this time — your local files are the source of truth). Copy the SSH URL GitHub gives you, then:

Bash
git remote add origin git@github.com:yourusername/my-project.git
git branch -M main
git push -u origin main

Your project is now live on GitHub.

Writing Commit Messages Like a Professional

Commit messages are the permanent documentation of every decision ever made in a codebase. A well-written history lets you debug, audit, and onboard teammates in minutes instead of hours.

The Anatomy of a Great Commit Message

A commit message has two parts: a subject line (mandatory) and a body (optional but powerful):

Bash
feat: add two-factor authentication to login flow

Users with sensitive accounts requested 2FA. This change adds
TOTP support via the `speakeasy` library. The QR code is displayed
on the /settings/security page.

Closes #214

Rules for the subject line:

  • Keep it under 72 characters — GitHub truncates beyond this
  • Write in imperative mood: Add feature not Added feature or Adding feature
  • Do NOT end with a period
  • Prefix with a type (see Conventional Commits below)

The Conventional Commits Standard

Most teams follow Conventional Commits — a lightweight specification that makes commit history machine-readable and changelog-ready:

feat:

A new feature for the user. Triggers a minor version bump.

fix:

A bug fix. Triggers a patch version bump.

docs:

Documentation only. No code change.

refactor:

Code restructure. No behaviour change, no bug fix.

test:

Adding or fixing tests. No production code change.

chore:

Build tools, dependencies, config. Does not affect users.

Scope is optional but useful: feat(auth):, fix(payment): — scopes tell reviewers exactly which part of the app changed, which is invaluable in large codebases.

Knowledge Check

Ready to test your understanding of 3. Your First Repository?

Back