Your First Repository: init, add and commit
Create a Git repository, check its status, stage files with git add, save snapshots with git commit and read the history with git log.
On this page 12 sections
Introduction
Time to use Git for real. Throughout this course you will follow Priya as she builds and manages a small portfolio website. You can follow along on your own computer — the outputs shown here are exactly what Git printed when these commands were run.
- How to turn a folder into a Git repository with git init.
- How to read git status, the command you will use most.
- How to stage changes with git add and save them with git commit.
- How to view your project’s history with git log.
Create a Repository
Create a folder for the project, move into it, and run git init. This creates a hidden .git folder where Git will store the entire history. Everything else in the folder stays exactly as it is.
mkdir my-portfoliocd my-portfoliogit initClick Run to see what this code prints.
The .git folder is Git’s database. Do not change or delete files inside it by hand. Deleting the .git folder deletes the project’s entire history (your working files stay, but every commit is gone).
Check the Status
git status tells you what is going on in your repository: which branch you are on, what has changed and what Git suggests doing next. Run it whenever you are unsure — it never changes anything.
git statusClick Run to see what this code prints.
Now create the home page. Save this as index.html in the my-portfolio folder:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Priya Sharma</title></head><body> <h1>Hi, I'm Priya</h1> <p>I'm learning web development.</p></body></html>git statusClick Run to see what this code prints.
Git has noticed the new file but lists it as untracked: it is in your working directory, but Git is not recording its history yet. Git never starts tracking a file on its own — you decide what goes into the project.
Stage a File
git add copies the current version of a file into the staging area — the list of changes that will go into your next commit.
git add index.htmlgit statusClick Run to see what this code prints.
The file is now listed under “Changes to be committed”. Notice that git add itself printed nothing — and that git status tells you how to undo the staging if you change your mind.
Make Your First Commit
git commit saves everything in the staging area as a new snapshot. The -m option supplies the commit message directly; without it, Git opens your editor so you can write one.
git commit -m "Add home page"Click Run to see what this code prints.
Let’s read that output. main is the branch the commit was made on, and root-commit means it is the very first commit, with no parent. 55024d8 is the short form of the commit’s unique ID. The last lines summarise the change: one file with 11 new lines, created with normal file permissions (100644).
git statusClick Run to see what this code prints.
“Working tree clean” means every file in your folder matches the latest commit. This is the state to aim for before switching tasks.
You edit index.html and save it, but do not run any Git commands. Where does git status list the file?
Commit More Files
Add a stylesheet, style.css:
body { font-family: sans-serif; margin: 40px;}Instead of naming each file, you can stage everything that changed in the current folder with git add . (the dot means “this folder”). It is convenient, but always check git status first so you do not stage something by accident.
git add .git commit -m "Add basic styles"Click Run to see what this code prints.
Read the History
git log lists commits, newest first, with the full ID, author, date and message:
git logClick Run to see what this code prints.
The label (HEAD -> main) next to the newest commit tells you where you are: you are on the main branch, and main currently points to this commit. You will learn exactly what that means in the lesson on branches. For a quick overview, --oneline shows one line per commit with the short ID:
git log --onelineClick Run to see what this code prints.
When the history is longer than your screen, Git shows it in a pager. Use the arrow keys or Space to scroll, and press q to quit and return to the prompt.
Put the steps for adding a new file to a repository in order.
- Confirm with git log --oneline
- Save the snapshot with git commit -m "message"
- Stage the file with git add
- Create the file in the project folder
- Check what changed with git status
Writing Good Commit Messages
A commit message explains why a change was made, for your teammates and for you in six months. Most teams follow a few simple rules:
- Write a short summary line, ideally under about 50 characters.
- Use the imperative mood, as if giving an order: “Add home page”, not “Added home page” or “Adding home page”.
- Describe what the commit does, not how you felt: “Fix broken contact link” beats “oops”.
- Keep one logical change per commit, so each message can stay short and accurate.
Common Mistakes
- Running git init in your home folder or Desktop instead of the project folder — that tracks far too much.
- Forgetting git add, then wondering why git commit says “nothing added to commit”.
- Using git add . without checking git status first and committing files you did not mean to.
- Writing vague messages like “update” or “changes” that will mean nothing later.
Frequently Asked Questions
Yes — once per project, in the project’s top folder. You do not need it when you download an existing repository with git clone (lesson 10), because that already includes the .git folder.
In modern Git, both stage new, modified and deleted files. git add . is limited to the current folder and its subfolders; git add -A covers the whole repository.
Whenever you reach a small, working step you could describe in one sentence. Several commits an hour is normal while you are actively working.
No. Git tracks files, not folders. A folder appears in the repository as soon as it contains a tracked file; people often add an empty .gitkeep file for this.
Key Takeaways
- git init turns a folder into a repository by creating the hidden .git folder.
- git status shows what changed and what Git suggests next — run it often.
- git add stages changes; git commit -m "message" saves them as a snapshot.
- git log shows the history; git log --oneline gives a compact summary.
- Good commit messages are short, imperative and describe one logical change.
Summary
Priya’s portfolio now has a home page and a stylesheet, each saved as its own commit. Next you will learn to see exactly what changed — line by line — before and after you commit, and to dig through history to find out when and why something changed.