Developer tools
Git: commits, branches, history
A time machine for code: a project history made of snapshots, branches for experiments, and a way to share work with a team.
Why it exists
Without version control, code lives in folders called site-final, site-final-2, site-REALLY-final, and any "quick tidy-up" can cost a day of work. Nobody knows who changed a line or why, you can't safely try a risky idea, and two people editing one file overwrite each other. Git solves all of it at once: every commit is a point you can return to; branches give ideas a safe sandbox; merging combines several people's work and tells you honestly when edits collide.
Where it's used
- Practically every software project: from a learning website to the Linux kernel, which git was written for in 2005.
- Teamwork through GitHub and GitLab: a branch per task, a pull request, code review, automatic tests before merging.
- Deployment: services like Vercel and Netlify publish the site on every
git push, and a rollback is a return to an earlier commit. - Finding when a bug appeared:
git log,git blame(who changed a line, and when) andgit bisect(binary search through history).
Syntax
git status # what changed
git add cart.js # stage it
git commit -m "Why" # save a snapshot
git log --oneline # history
git switch -c new-cart # new branch
git merge new-cart # merge a branch
git push / git pull # sync with the server
git revert <id> # undo a commit with a new commitExamples
// A JavaScript model of git: a commit is a snapshot plus a link to its parent
const commits = {
a1: { parent: null, message: "Shop skeleton" },
b2: { parent: "a1", message: "Cart" },
c3: { parent: "b2", message: "Discounts" },
};
const branches = { main: "c3" };
function log(id) {
const messages = [];
for (; id; id = commits[id].parent) messages.push(commits[id].message);
return messages;
}
log(branches.main); // → ["Discounts", "Cart", "Shop skeleton"]function hash(text) {
let h = 0;
for (const ch of text) h = (h * 31 + ch.charCodeAt(0)) % 16777216;
return h.toString(16).padStart(6, "0");
}
hash("discount 10%"); // → "a93687"
hash("discount 10%") === hash("discount 10%"); // → true — same content, same address
hash("discount 15%") === hash("discount 10%"); // → falseconst branches = { main: "c3" };
branches["new-cart"] = branches.main; // git branch new-cart
branches["new-cart"] = "d4"; // a commit on new-cart moves only new-cart
branches.main; // → "c3"
Object.keys(branches); // → ["main", "new-cart"]Results after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
Inside .git live objects, and each one's name is the hash of its content (SHA-1, 40 hexadecimal characters). A file is a blob object, a folder is a tree (a list of "name → hash"), a commit is an object with the tree's hash, the parent's hash, the author, the date and the message. Identical content gives an identical hash, so unchanged files aren't duplicated between commits.
A commit stores a snapshot, not changes: git diff is computed on the fly by comparing two snapshots. A commit's hash depends on its parent's hash, so changing any old commit changes the hashes of all its descendants — forged history shows up immediately.
A branch is a file holding one commit's hash (.git/refs/heads/main). HEAD points to the current branch. A new commit writes its hash into the current branch — that's how a branch "moves". A merge finds the common ancestor of two branches (the merge base) and compares both sides with it; if both changed the same place — a conflict.
Between the working tree and the history sits the staging area: git add puts there what goes into the next commit. That lets you commit two of ten changed files as a separate, meaningful step.
Traps
When to pick something else
Git is a poor fit for large binary files — videos, datasets, build outputs: every version is stored whole and the repository balloons (there's Git LFS or plain file storage for those). And git isn't a safe: passwords and keys don't belong in it, not even in a private repository.
Practice
Lessons where this appears
- The lost TuesdayTale · Chapter 28 · Git: the history of code
- Why does git store snapshots, not changes?Why? · Chapter 28 · Git: the history of code
- Build your own mini-gitWorkshop · Chapter 28 · Git: the history of code
- Resolve the conflictLab · Chapter 28 · Git: the history of code
- The Great ConflictBoss · Chapter 28 · Git: the history of code
- How a real app gets plannedTale · Chapter 29 · Final project: a weather app