Git Essentials
The shop's config lives in /srv/shop and nobody knows what changed. Put it in git: status, add, commit, branches, ignore rules, undo, and sharing through a remote.
An interactive Linux lesson: 21 steps, about 30 minutes, on a live simulation in your browser.
You are alice, and you look after web01, the machine that serves the shop's website. The site's config lives in /srv/shop: a price list, the homepage banner and the payment key. On Monday the site shows the wrong price for bread, and both you and bob edited the files over the weekend.
Look at the listing. It shows names, sizes and times. Nothing in it says who changed the bread price, what it was before, or why anyone touched it. The files hold the current state. The story of how they got there is gone.
What you will learn
Nobody knows what changed
- Monday morning, wrong price: Files remember their current state. Only a kept history remembers who changed what, and why.
- What stat can and cannot say: A timestamp says when. An owner says who owns. Neither says who edited, what changed, or why.
Your first repository
- git init: start keeping history: A repository is your files plus a .git directory that remembers every version.
- Three places, not one: Git sees three places: the working tree you edit, the staging area the next commit will hold, and the history already recorded.
- add, then commit with a message: A commit is a snapshot of the staging area plus a message that says why it exists.
- The loop: change, stage, commit: Change the files, check status, stage what belongs together, commit with a message. Small commits hold one idea each.
The daily loop
- Review with diff before you add: Diff before you add. Minus is recorded, plus is yours, and the shape of the change should match what you meant.
- Drill: history on one line each
- Drill: the short status
- What does git add . catch?: add . stages every change it can see. Check status after, every time, before you commit.
Ignore the generated
- A .gitignore for the noise: Ignore what is generated, commit what is handmade. An ignored file stays on disk and leaves the status alone.
- .gitignore cannot forget: .gitignore stops new files. It never forgets a file git already tracks; rm --cached untracks while keeping the file.
Branches are labels
- A branch is a movable label: A branch is a movable label stuck on a commit. Committing moves only the label you are standing on.
- Merge: bring the label forward: A merge moves your label forward when nobody else moved. History stays one straight story.
- Break it: commit with no branch: A detached HEAD has no branch label, so commits made there are lost unless you name a branch for them.
- Break it: committed on main: reset --soft moves the branch label back and keeps your work staged. The commit is undone, the work is not.
Sharing through a remote
- A remote, and fetching from it: Fetch updates your copy of their history and touches none of your files. Pull is fetch followed by a merge.
- Break it: push says no: Push only moves the remote forward. When it says no, fetch their work and replay yours on top of it first.
- Undo with restore, unstage too: restore discards what you name in the files. restore --staged unstages and leaves the files alone.
Recap & playground
- Cheat sheet
- Playground