Git — Saving Snapshots
You change the bakery's menu layout, and it looks worse. You want yesterday's version back, but you have already saved over the file. Or a friend helps with the CSS and you both edit styles.css at once. Version control solves both problems, and Git is the tool almost every team uses for it.
The rule
Edit, stage, commit: each commit is a snapshot you can always return to.
A Git repository is your project folder plus a hidden .git folder where Git keeps the full history. Start one inside the project:
git initGit thinks in three places:
| Place | What it holds |
|---|---|
| Working directory | Your files as they are now, being edited |
| Staging area | The changes you have chosen for the next snapshot |
| Repository | The saved snapshots, called commits |
The everyday loop:
git status # what changed, and what is staged?
git add index.html styles.css # stage these changes
git commit -m "Add opening hours to the footer"
git log --oneline # list recent commitsgit statusis your dashboard. Run it often.git addmoves changes into the staging area.git add .stages everything in the current folder.git commitsaves the staged changes as a new snapshot with a message. Each commit gets a unique ID (a long hash, often shortened to 7 characters likea1b2c3d), and records the author, the time and a link to the commit before it.git diffshows unstaged changes line by line;git diff --stagedshows what is about to be committed.
Why a staging area? It lets you split work into meaningful commits. If you fixed a typo and redesigned the cards in the same sitting, you can stage and commit the typo fix alone, then the redesign.
Good commit messages say what the change does, in the present tense: "Add allergy notice to menu", not "stuff" or "fixed things". Months later, the log should read like a story of the project.
Some files should never be committed: installed packages, build output, secret keys. List them in a .gitignore file and Git will ignore them.
The shortcut
Before committing, run the "one sentence" test: can you describe the staged changes in one short sentence without the word "and"? If not, you are probably mixing two changes; stage them separately.
Look
git statusshowsindex.htmlandstyles.cssmodified.
The trap is editing a file, running git commit and wondering why nothing was saved. Only staged changes go into a commit.
Drills
Answer fast — trust the rule.
You edited menu.html and ran git commit -m "Update menu" without git add. What happens?
Which command shows which files are modified and which are staged?
Which is the best commit message?
Where should you list folders that Git must never track, like installed packages?
Remember
- Edit, stage, commit: each commit is a snapshot you can always return to.
- One sentence test: if you need "and", make two commits.
- Trap: unstaged changes are not in the commit.
