Git from zero: snapshots of your work
Lesson 1 of 3
1Git from zero: snapshots of your work
The big pictureWithout Git you end up with "final2_edited_LAST.docx". With Git you have a time machine: every version is kept, and you know who changed what and why.
What the picture shows: Three areas: the working folder where you edit, the staging area where you pick what goes in the snapshot, and the repository where snapshots are kept for good.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
Picture a photo album of your project. Each time you finish a step you take a photo (commit) and write a caption. Before the shot you choose who stands in it (add). If you break something you go back to the previous photo. A branch is a side album where you try an idea without touching the main one.
Git is a distributed version control system: every copy of a repository carries the full history. A commit is an object pointing to a tree (a full snapshot of the files) and to one or more parents, and its id is a SHA hash of its content. A branch is just a movable pointer to a commit, and HEAD points at the current branch. The staging area (index) lets you build a precise commit out of part of your changes.
Before You Start
⌨️The terminal
You know cd and ls from the toolkit track.
The Core Idea
1A repository and its first commit
git init turns a folder into a repository. git status tells you what changed. git add stages files, and git commit -m "..." takes the snapshot.
git init notes cd notes echo "first idea" > ideas.txt git status # ideas.txt is untracked git add ideas.txt git commit -m "Add the first idea" git log --oneline # one commit
2The staging area: choose what goes in
Changed three files but only one is ready? Add just that one and commit. A small commit with a clear message is easier to review and to undo.
git add . then a commit called "changes". Secret or temp files slip in, and a month later nobody knows what changed. Write a .gitignore and a message that says why.3History and diffs
git log shows the snapshots, and git diff shows what changed line by line before you stage it. A line starting with + was added; one with - was removed.
4Branches: experiment safely
git switch -c login-page creates a branch and moves to it. You work there freely while main stays intact. When done you merge it (git merge); if the idea fails you delete it.
git switch -c login-page echo "<form>...</form>" > login.html git add login.html git commit -m "Add the login form" git switch main # login.html is not here git merge login-page # now it is
Worked example
Problem: you deleted an important paragraph from report.md and staged nothing yet. Fix: git diff report.md shows the removed lines with -, and git restore report.md puts the file back as it was at the last commit. If you had staged it: git restore --staged report.md first.
Myth vs Reality
| Myth | Reality |
|---|---|
| Git and GitHub are the same. | Git is a program on your machine. GitHub is a site that hosts repositories and adds collaboration. The next lesson covers it. |
| Git is only for code. | It suits any text files: notes, docs, settings, even a book. |
Test your understanding
What does git add do?
add stages; commit is what takes the snapshot.
Why work on a branch instead of main?
A branch is a safe space. main stays runnable until you merge finished work.
Which commit message is better?
A good message says what changed and why, so a teammate understands it a year later.
Quick review cards
Check Your Understanding
What is the difference between git add and git commit?
add picks changes into staging; commit saves what is staged as a permanent snapshot with a message.
How do you return a file to the last commit?
git restore file if it is not staged; if it is, git restore --staged file then git restore file.
Why write a .gitignore on day one?
Because a secret that enters the history stays there even if you delete it later.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Done with this lesson?
Sign in to save your progress.