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 shootGitWhat it is
The roomWorking directoryThe files as they're being edited
The stageStaging area (or index)The changes picked for the next commit
The albumRepositoryThe saved commits, in .git
  1. Working directory

    The room

    git add
  2. Staging area

    The stage

    git commit
  3. 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

Terminal
git add notes.txt
git status
Output
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   notes.txt

Changes to be committed means it's on the stage.

2.Commit it

Terminal
git commit -m "Add notes file"
git status
Output
[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 clean

That'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

Terminal
echo "Learning the three areas" >> notes.txt
git status
Output
Changes not staged for commit:
        modified:   notes.txt

Git 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

Terminal
git add notes.txt
echo "Third line" >> notes.txt
git status
Output
Changes to be committed:
        modified:   notes.txt

Changes not staged for commit:
        modified:   notes.txt

The 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

Terminal
git restore --staged notes.txt
git status
Output
Changes not staged for commit:
        modified:   notes.txt

The 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

Terminal
git add notes.txt
git commit -m "Add more notes"
Output
[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
A Mac terminal: notes.txt untracked, staged with git add, committed as Add notes file, then a working tree clean; a new line makes it modified, it's staged, and a third line makes it appear as both staged and not staged.A Mac terminal: git restore --staged notes.txt unstages the file, then git add and git commit -m Add more notes create a second commit and the working tree is clean.
The steps above, run for real, in order.

Ways to stage

CommandStages
git add notes.txtOne file
git add file1 file2Several files
git add .Everything in the current folder and below
git add -pParts of a file, piece by piece, so two changes in one file can go into different commits

File states

StateMeaningArea
UntrackedA new file Git has never seenWorking directory
ModifiedChanged, but not stagedWorking directory
StagedReady for the next commitStaging area
CommittedSavedRepository