Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Version Control and Hosting  ›  Lesson

Git Basics — Snapshots and the Staging Area

Git 12 minGit records changes by staging chosen edits from the working tree with add and saving them as a commit, a snapshot linked to its parent.

Git Basics — Snapshots and the Staging Area

Last week someone changed the notes-api config on web1 by hand, the service broke, and nobody could say what the file looked like before. With Git, every change is a recorded snapshot with an author, a date and a reason, and going back takes one command.

The rule

Edit in the working tree, choose with add, record with commit.

Git keeps your project in three places:

  1. Working tree: the files you see and edit.
  2. Staging area (the index): the changes you have chosen for the next snapshot.
  3. Repository: the history of commits, each a full snapshot of the staged files.
git init                         # start tracking this folder
git status                       # what changed, what is staged
git add config.yaml              # stage this file's current content
git commit -m "Raise worker count to 4"
git log --oneline                # a1b2c3d Raise worker count to 4

Every commit has an ID (a hash such as a1b2c3d), an author, a timestamp, a message, and a pointer to its parent commit. That chain of parents is your history.

Two diff commands match the two gaps between the areas:

  • git diff shows working tree changes not yet staged.
  • git diff --staged shows what will go into the next commit.

A .gitignore file lists things Git should never track: virtual environments, build output, and above all files with secrets, such as .env.

The shortcut

For any change, ask "where does it live right now?" Modified (working tree only), staged (chosen), or committed (recorded). git status answers it, and the next command is always the one that moves the change one step right: add to stage, commit to record.

Recording a config changeStep 1 of 4
  1. Edit

    You change workers: 2 to workers: 4 in config.yaml. git status lists it as modified.

The trap is git add . without looking. It stages everything, including stray logs or a .env file with passwords. Check git status first, and keep a good .gitignore.

Drills

Answer fast — trust the rule.

quick drill

You edited app.py but have not run git add. Where is the change?

quick drill

Which command shows the changes that will go into your next commit?

quick drill

What does every commit point to, forming the project's history?

quick drill

Where should a file of local passwords such as .env be listed?

Remember

  • Edit in the working tree, choose with add, record with commit.
  • Shortcut: ask where the change lives, then run the command that moves it one step.
  • Trap: git add . without checking git status, and committing secrets.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhere does the change live?

You changed config.yaml and also have a new .env file with a database password in the same folder. Explain how you would commit only the config change, and how you would keep the .env file out of Git for good.