The lost Tuesday
Repository, staging and commits, branches and merges, GitHub and pull requests, .gitignore and undoing safely.
cart-final-2-REALLY-final.js turned out not to be final at all. Today we talk about a time machine for code. Its name is git.Git is a version control system. It keeps a project's history as a chain of commits. A commit is a snapshot of every file at some moment, plus a label: who, when, and why. A project folder together with its history is a repository: git init creates a hidden .git folder inside it, and that's where the history lives.
Git has three "zones":
- the working tree — the files you see and edit;
- the staging area (the index) — a draft of the next commit: exactly what you've decided to save;
- the history — saved commits. Nobody rewrites them; new ones are only added on top.
A normal working loop in the terminal (these are terminal commands, not JavaScript):
- git status — what changed and what's already staged;
- git diff — line by line: what it was and what it is now;
git add cart.js— stage a file's changes;git commit -m "Cart: discount never drives the total negative"— save a snapshot with a message;- git log --oneline — the history, newest first:
c3e9f0 Cart: discount…,7d02b1 Add the cart.
const workingTree = { "cart.js": "discount 10%", "notes.txt": "buy cat food" };
const staging = {};
const history = [];
// git add cart.js
staging["cart.js"] = workingTree["cart.js"];
// git commit -m "Cart: 10% discount"
history.push({ message: "Cart: 10% discount", files: { ...staging } });
console.log(history.length);
console.log(Object.keys(history[0].files));notes.txt changed but wasn't added — it's not in the snapshot.A branch is just a name that points to a commit. The main one is usually called main. Want to try a risky idea? git switch -c new-cart creates a branch and switches to it. New commits move only new-cart forward; main stays put. Didn't work out — git switch main, and everything is as it was.
The idea worked — git merge new-cart merges the branch into the current one. If the branches changed different places, git combines them itself. If both changed the same line, you get a merge conflict: git doesn't guess who's right, it honestly puts both versions into the file, with markers.
const conflict = [
"<<<<<<< HEAD",
"const discount = 0.1;",
"=======",
"const discount = 0.15;",
">>>>>>> new-cart",
].join("\n");
console.log(conflict);<<<<<<< HEAD and ======= is your version; below it, down to >>>>>>> new-cart, is the one coming from the branch.Resolving a conflict means editing the file by hand: keep the right code (or build a third version from both), delete all three markers, then git add and git commit. Editors help with "Accept current / incoming / both" buttons — in the lab you'll write them yourself.
So far all of this lived on your machine. A remote is a copy on a server, most often on GitHub or GitLab. git push sends your commits there, git pull fetches other people's. Teams don't push straight to main: you make a branch, push it and open a pull request — a "please merge this". Teammates read the diff and leave comments (that's code review), a server runs the tests — and only then the branch is merged.
Almost anything can be undone:
git restore cart.js— throw away unsaved edits to a file and bring back the version from the last commit (those edits are gone for good);git revert c3e9f0— create a new commit that undoes an old one. The history stays honest, nothing breaks for teammates;git reset --hard 7d02b1— move the branch back, "forgetting" commits. Dangerous on a branch you've already pushed: teammates still have those commits, and the histories diverge.
git restore cart.js would have brought back the version from the 5:55 pm commit. If I'd made it, of course. Moral: commit often, in small steps, with clear messages. A commit costs pennies. A lost day doesn't.Pop quiz
Mistakes are fine: keep trying until you find the right answer, and Byte explains why it's right.