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:
- Working tree: the files you see and edit.
- Staging area (the index): the changes you have chosen for the next snapshot.
- 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 4Every 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 diffshows working tree changes not yet staged.git diff --stagedshows 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.
Edit
You change
workers: 2toworkers: 4inconfig.yaml.git statuslists 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.
You edited app.py but have not run git add. Where is the change?
Which command shows the changes that will go into your next commit?
What does every commit point to, forming the project's history?
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 checkinggit status, and committing secrets.
