The Plain Picture#
Taking a group photo isn't instant. First you arrange who's in the shot ("you, you, and you — step in"), then you press the button. Some people in the room might not be in this photo.
The Real Names#
A repository (repo) is a project folder that Git tracks. Git keeps all its history in a hidden folder called
.gitinside it.The working directory (working tree) is the normal files you see and edit.
The staging area (also called the index) is the "arranging people for the photo" step: the exact set of changes that will go into the next commit.
A commit is the photo: a saved snapshot of the staged files, with a message, author and time.
WORKING DIRECTORY STAGING AREA REPOSITORY (.git)
(files you edit) (what the next commit (all saved commits)
will contain)
│ │ │
│ ──────── git add ───────────▶ │ │
│ │ ──────── git commit ───────▶ │
│ ◀─────────────────────── git restore / git switch ────────── │Under the Hood — a real first session#
First, tell Git who you are (once per computer):
git config --global user.name "Priya"
git config --global user.email "priya@example.com"Then:
mkdir ~/git-practice && cd ~/git-practice
git init -b main # create a repo; the first branch is called "main" (Ch 4)Create quiz.py:
def score(correct):
return correct * 10git status --short # → ?? quiz.py (?? = untracked: Git sees it but isn't tracking it)
git add quiz.py # stage it
git status --short # → A quiz.py (A = added to staging)
git commit -m "Add quiz scoring"
git log --oneline # → 7ef9500 Add quiz scoring7ef9500 is the start of the commit's ID (Ch 3).
Why a staging area? It lets you commit part of your work. If you fixed a bug and started a new feature in
the same afternoon, you can git add only the bug-fix files and commit them alone, with a clear message — then commit
the feature separately. git add -p goes further: it lets you pick individual chunks of changes inside one file.
Status codes you'll see in git status --short:
| Code | Meaning |
|---|---|
?? | untracked — new file Git has never been told about |
A | added (staged, new file) |
M (left column) | modified and staged |
M (right column) | modified but not staged |
D | deleted |
UU | unmerged — a conflict to resolve (Ch 5) |
Good commit messages: a short summary line in the imperative mood ("Add bonus points", not "added stuff"), under
about 50 characters, then optionally a blank line and a longer explanation of why. Future-you, reading git log at
11 p.m. during an incident, will be grateful.
Gotchas
git commit -am "msg"stages and commits all changes to already-tracked files in one go — but it does not add new untracked files.Commit small and often. Small commits are easy to review, easy to revert and easy to search (Ch 11).
Check yourself
4 questionsName the three areas a change moves through.
Show answerHide answer
Working directory → staging area → repository.
What does git add do? git commit?
Show answerHide answer
add puts changes into the staging area; commit saves the staged snapshot into history.
Why is the staging area useful?
Show answerHide answer
It lets you choose exactly which changes go into each commit, so unrelated work can be committed separately.
What does ?? mean in git status --short?
Show answerHide answer
The file is untracked — Git sees it but has never been told to track it.