Git and GitHub · Part 1 · Git basics
The three areas
Like a family photo shoot
This is the most important idea in Git. Once it clicks, most commands make sense.
Think of a family photo. Everyone is moving around the house (the room). The photographer calls some of them in front of the camera (the stage), while Grandpa stays in the kitchen. The camera clicks, and that moment goes into the album for good. Git works the same way:
| Photo shoot | Git | What it is |
|---|---|---|
| The room | Working directory | The files as they're being edited |
| The stage | Staging area (or index) | The changes picked for the next commit |
| The album | Repository | The saved commits, in .git |
- git add
Working directory
The room
- git commit
Staging area
The stage
Repository
The album
Why there's a staging area
Imagine an hour of work that fixed a navbar bug, fixed a typo, and started a footer that's still broken. The two fixes are ready; the footer isn't. The staging area lets the fixes go into a commit now and the footer wait, just as the photographer picks who is in the photo.
Walking a file through the three areas
This continues in git-practice, where notes.txt is untracked: in the room, not on the stage.
1.Stage it
git add notes.txt
git statusChanges to be committed:
(use "git rm --cached <file>..." to unstage)
new file: notes.txtChanges to be committed means it's on the stage.
2.Commit it
git commit -m "Add notes file"
git status[main (root-commit) 2c12ea6] Add notes file
1 file changed, 1 insertion(+)
create mode 100644 notes.txt
On branch main
nothing to commit, working tree cleanThat's the first commit. -m sets its message, and 2c12ea6 is its unique ID. Working tree clean means all three areas match.
3.Change the file
echo "Learning the three areas" >> notes.txt
git statusChanges not staged for commit:
modified: notes.txtGit tracks the file now, so it's modified, not untracked. Only the room changed. (>> adds a line to the end of a file; a single > would replace the whole file.)
4.Stage it, then edit it again
git add notes.txt
echo "Third line" >> notes.txt
git statusChanges to be committed:
modified: notes.txt
Changes not staged for commit:
modified: notes.txtThe same file appears twice. The stage has it with line 2, from when git add ran; the room also has line 3.
5.Take it off the stage
git restore --staged notes.txt
git statusChanges not staged for commit:
modified: notes.txtThe file is off the stage, and both new lines are still in it. Nothing is lost. (Before the first commit, Git's hint says git rm --cached instead; it does the same job.)
6.Stage and commit
git add notes.txt
git commit -m "Add more notes"[main dbad70d] Add more notes
1 file changed, 2 insertions(+)Both lines are in the second commit, and the working tree is clean again.
See these steps in a real terminal


Ways to stage
| Command | Stages |
|---|---|
git add notes.txt | One file |
git add file1 file2 | Several files |
git add . | Everything in the current folder and below |
git add -p | Parts of a file, piece by piece, so two changes in one file can go into different commits |
File states
| State | Meaning | Area |
|---|---|---|
| Untracked | A new file Git has never seen | Working directory |
| Modified | Changed, but not staged | Working directory |
| Staged | Ready for the next commit | Staging area |
| Committed | Saved | Repository |