Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Version Control with Git  ›  Lesson

Git — Saving Snapshots

Git 14 minGit records a project's history as commits: you edit files in the working directory, stage the changes you want, and commit them as a snapshot with a message.

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 init

Git 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 commits
  • git status is your dashboard. Run it often.
  • git add moves changes into the staging area. git add . stages everything in the current folder.
  • git commit saves the staged changes as a new snapshot with a message. Each commit gets a unique ID (a long hash, often shortened to 7 characters like a1b2c3d), and records the author, the time and a link to the commit before it.
  • git diff shows unstaged changes line by line; git diff --staged shows 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.

Committing the day's workStep 1 of 4
  1. Look

    git status shows index.html and styles.css modified.

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.

quick drill

You edited menu.html and ran git commit -m "Update menu" without git add. What happens?

quick drill

Which command shows which files are modified and which are staged?

quick drill

Which is the best commit message?

quick drill

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.

Practice — level up

0/1 solved

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

ExplainOne change per commit

You fixed a typo in index.html and redesigned the card colours in styles.css in the same sitting. Explain how you would save this work with Git, which commands you would run, and why.