What you can do after this lesson
You will understand the single most important idea in Git, the three areas a change passes through. Once this clicks, add, commit, restore and reset stop being a pile of commands to memorise. We use a few commands here to illustrate; the next lesson walks through them slowly, with you typing along.
Three places, not two
Most people expect two places: the files on disk and the saved history. Git has a third in the middle.
- Working tree (also called the working directory). The ordinary files and folders you open in your editor. Changes here are just edits; Git notices them but has recorded nothing.
- Staging area (also called the index). A holding area listing exactly what will go into the next commit. Think of packing a parcel: you choose items and put them in the box.
- Repository. The permanent history of commits, kept in a hidden folder named
.gitat the top of your project. Think of the sealed, labelled parcel placed on the shelf.
A change normally travels left to right:
working tree --git add--> staging area --git commit--> repositoryand several commands move changes the other way, which later lessons cover.
Why the middle step exists
Why not commit straight from the working tree? Because real work is messy. Imagine you fixed a typo in the menu and, in another file, started a half-finished discount feature. Those are two separate ideas. With a staging area you can stage only the typo fix, commit it with a clear message, and leave the unfinished work for later. The history stays tidy: one commit, one purpose.
If you want a simple workflow, git commit -a stages and commits every change to already tracked files in one step. It is convenient, but it skips the chance to choose.
Tracked and untracked files
Git sorts files into two groups. Tracked files are ones Git already knows from the last commit or from the staging area. Untracked files are new ones Git has seen but has not been told to include. A new file stays untracked until you git add it, which is why a file you created never shows up in history until you do.
Watching a file move
Here is a real session in a practice repository. The prompt symbol is left out; lines starting with $ are commands.
git init
echo "Sourdough, rye and seeded loaf." > menu.txt
git statusOn branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
menu.txt
nothing added to commit but untracked files present (use "git add" to track)The file exists in the working tree only. Now stage it and ask again, this time with the short format:
git add menu.txt
git status -sA menu.txtThe A in the left column means "added to the staging area". Short status prints two columns: the left column describes the staging area compared with the last commit, and the right column describes the working tree compared with the staging area. Commit it:
git commit -m "Add the bakery menu"
git status[main (root-commit) 5bf5647] Add the bakery menu
1 file changed, 1 insertion(+)
create mode 100644 menu.txt
On branch main
nothing to commit, working tree cleanHashes such as 5bf5647 differ on every machine. "Working tree clean" means all three areas agree.
The staging area is a snapshot
This catches nearly everyone once. Suppose you stage a file and then edit it again before committing:
echo "Add a cinnamon bun." >> menu.txt
git add menu.txt
echo "Add a rye roll." >> menu.txt
git status -sMM menu.txtTwo M letters: modified in both columns. The left M is the staged version (with the bun); the right M says the working tree has a further change (the roll) that is not staged. Committing now would record the bun only. To include the roll you must git add menu.txt again. Git stores the content at the moment you ran add, not the filename.
Looking inside .git
The repository lives in .git. Do not edit its contents by hand, but a quick look makes it less mysterious:
ls .git
cat .git/HEADCOMMIT_EDITMSG
config
description
HEAD
hooks
index
info
logs
objects
refs
ref: refs/heads/mainindex is the staging area, objects holds your files' content and commits, refs holds branch labels, and HEAD says which branch you are on. Deleting .git deletes the whole history and turns the folder back into ordinary files, so never do that casually.
Common mistakes
- Assuming save means commit. Saving a file changes only the working tree.
- Editing after
git addand forgetting to stage again. Usegit statusbefore every commit to spot aMM. - Using
git add .blindly. It stages everything, including files you never meant to include. Read the status first. - Thinking an untracked file will be committed with
-a.git commit -acovers tracked files only. - Deleting
.gitto "reset" things. That destroys the history.
Exercise
- Create a new folder, run
git init, and create two files,a.txtandb.txt. - Stage only
a.txtand rungit status -s. What do you expect for each file? - Edit
a.txtagain and check status once more. - Commit, then check status a final time.
Show one possible answer
After staging only a.txt, short status shows A a.txt and ?? b.txt. After editing a.txt again it shows AM a.txt: added in the staging area and modified in the working tree. After git commit -m "Add a", a.txt has the staged version recorded; the later edit is still unstaged, so status shows M a.txt and ?? b.txt.
Key points
- The working tree holds the files you edit, the staging area (index) holds what the next commit will contain, and the repository holds committed history.
- git add copies a file's current content into the staging area; git commit turns the staged content into a permanent snapshot.
- The staging area lets you build a commit from only some of your changes.
- git status tells you what is untracked, modified, staged or clean.
- Staging records the file as it was when you ran git add, so later edits need another git add.