3. Your First Repository
Your First Repository
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:
See the hidden .git folder Git created. You'll see a .git directory — that IS the repository:
📋 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:
Files you are actively editing. Changes are not yet tracked by Git. This is your normal file system.
Files you have marked as ready to be committed. git add moves files here. Think of it as a loading dock.
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.
Step 2: Create a file.
Step 3: Check status again. README.md is now 'untracked'.
Step 4: Stage the file. Add it to the staging area. To stage ALL changed files at once, use git add .
Step 5: Commit the staged changes with a message.
Step 6: View the history. This shows each commit: hash, author, date, and message.
📝 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
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:
- Go to github.com → click the + icon → New repository
- Give the repo a name (e.g.,
my-project). Use lowercase and hyphens — no spaces. - Choose Public (visible to anyone) or Private (only you + collaborators).
- Check Add a README file so the repo is not empty.
- Click Create repository.
GitHub now shows you the repo. To bring it to your local machine:
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:
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:
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):
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:
A new feature for the user. Triggers a minor version bump.
A bug fix. Triggers a patch version bump.
Documentation only. No code change.
Code restructure. No behaviour change, no bug fix.
Adding or fixing tests. No production code change.
Build tools, dependencies, config. Does not affect users.
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?