Your First Repository: init, add, commit
A repository (repo) is a project folder that Git tracks. Saving a snapshot takes two steps: choose the changes (stage them), then commit them with a message.
A commit is a saved snapshot with a message describing what changed. The staging area lets you choose exactly what goes into each commit, so each snapshot tells one clear story - which makes history easy to read and changes easy to undo.
Example
mkdir my-portfolio
cd my-portfolio
git init
# Initialized empty Git repository in .../my-portfolio/.git/
echo "<h1>Riya Sharma</h1>" > index.html
git status
# Untracked files:
# index.html
git add index.html # stage this file
git commit -m "Add home page heading"
# [main (root-commit) 3f2a1c9] Add home page heading
# 1 file changed, 1 insertion(+)The three places
- Working folder - the files you edit
- Staging area - changes you've picked for the next commit (
git add) - Repository - the saved history (
git commit)
Staging lets you commit only some changes: fixed a bug and started a new feature? Commit the fix alone.
Example
git add . # stage everything changed in this folder
git add css/style.css # or one file
git commit -m "Style the header"Good commit messages
- Short summary in the present tense: "Add contact form", "Fix price rounding"
- One idea per commit - easy to understand and to undo
- Not: "changes", "final", "final2"
Git stores everything in the hidden
.git folder. Delete it and the history is gone - so never delete it by accident.Key points
- git init starts a repository.
- git add stages changes; git commit saves them.
- Write short, clear commit messages.
📝 Test yourself
1. Which command stages a file for the next commit?
2. Which is a good commit message?
3. Fill in the command that starts tracking a folder:
git
