Rzemiosło · The Craft
What is The CraftLevelsGet startedChapters Search Download PLEN
Rules of chapter 05
Level 2 · Technical Chapter deep-dive

In the core the rules are short and hard. Here is what stands behind them: the why, examples and stories from real projects. Rule quotes come straight from the core.

05 — Git and deployments

Chapter 05 is where mistakes cost the most: git is the project’s memory and the undo button for everything else, and a Deploy — Releasing a new version of the app “live”, where real people use it. It’s the moment a change reaches real people — which is why it happens only on an explicit “deploy”. more in the glossary → is the moment work reaches real people. Below is what stands behind the rules — commands, stories and the why. (The rehearsal on Preprod (staging) — A faithful copy of production with no real users, where you rehearse a deploy. A migration that fails on preprod is an ordinary Tuesday; in production it’s an incident. more in the glossary → has its own deep dive in chapter 18, the Database swap — Replacing the whole production database with a new version, carrying over user accounts and data. The hardest operation in The Craft — it has its own runbook and needs explicit consent. more in the glossary → in 19.)

History in a minute

Search the history before you implement: ↗

The full set that answers most “where did this come from” questions:

git log --oneline -- <path>         # history of a file or directory
git log -S"symbol" --oneline        # where a symbol / flag / column was born
git log -G"regex" --oneline         # commits whose DIFF matches a pattern
git log --grep="word" --oneline     # commit messages — features are described there
git blame <file> -L a,b             # who wrote these lines, when and why
git show <commit>                   # the full change with context

History + code + the directory’s AI_README — A map file in every code directory: what’s here, what it’s for and what to watch out for — written for search. The assistant finds the right file with one search instead of reading the whole directory. more in the glossary → = the full picture in a minute. It makes the biggest difference with unfamiliar code, with the hunch “didn’t we already do this once?”, with a column or flag of unknown origin, when dating a regression (“since when has this been broken?”) and before every refactor.

A clean table

Keep git status clean. ↗

Junk gets caught early, before it slips into a Commit — A saved, labelled point in the project’s history — a confirmation of a specific batch of changes. Small, described commits make a history you can return to any working state from. more in the glossary →: .bak files, temp files, stray databases, even a file named -w created by a mistyped curl — A command-line tool that sends a request to an address and shows the server’s response. The simplest proof that a page responds and what exactly it returns — no browser needed. more in the glossary → command. Patterns go into .gitignore — with one trap: .gitignore has no inline comments; # must sit on its own line, otherwise the pattern silently doesn’t work.

A second trap (Windows): git status shows a file as changed while git diff HEAD is empty. That’s only a line-ending difference (CRLF / LF (line endings) — Two ways of storing the end of a line in a file: Windows uses CRLF, Linux and macOS use LF. The difference makes git show “changed” files that nobody actually changed. more in the glossary →/LF), not work — committing such “noise” clutters the history and makes real changes harder to review.

Someone else’s work in the tree

Orphans: ↗

When several sessions work on one Repository (repo) — A store of the whole project’s code together with its full history of changes — who changed what and when. The repo is the project’s memory: code, documentation and change history in one place. more in the glossary → (you + Claude, or several Claude sessions at once), the working tree collects changes the current session didn’t make. Before a commit and a deploy the question is: is this finished, verified work (commit it deliberately, with confirmation), or an experiment or work in progress (leave it)? Sweeping someone else’s uncommitted work into your own commit is how a half-done experiment ends up in Environment (dev / prod) — Separate places where the app runs: “dev” for safe tests, “prod” for real users. Dev is a playground, prod is where real users are — the rules for each are completely different. more in the glossary →.

Commit messages

Commit messages in English ↗

A commit message is a name that tools print and that people search for (git log --grep) — hence English, like the rest of the code (17). A good message says what and why, and an Issue — A task or bug recorded in a tracker (e.g. GitHub Issues), with a description and acceptance criteria. That’s where the “why” of a change lives — not in someone’s head or a chat that will disappear. more in the glossary → number (refs #12, fixes #12) ties the commit to the context of the decision. PR (pull request) — A request to merge a branch into the main version, with a description of the changes and room for review. A small PR can be reviewed properly in fifteen minutes; a huge one goes through “on trust”. more in the glossary → and issues are linked by full URL, not a bare “PR #123” — such a shorthand means nothing outside one tool.

Undo: what actually happened

Commit early ↗

The idea that git is scary comes from one misunderstanding: people think a mistake is irreversible. It almost never is. You don’t need fluency — you only need to recognise which of six situations you’re in:

PanicWhat actually happenedThe way back
“I wrecked a file, didn’t commit”Only the working copy changedgit restore <file>
“I want to undo the last commit but keep the work”A commit is just a pointergit reset --soft HEAD~1
“I committed on the wrong Branch — A separate working version of the project where you change something on the side, without touching what already works. On a branch you experiment safely; only what’s been checked reaches the main version. more in the glossary →”The commit exists, just in the wrong placegit branch <right-one>, then git reset --hard HEAD~1 on the wrong one
“I deleted a branch / commits disappeared”Nothing disappeared — the label didReflog — Git’s log of every position your copy has been at — even after a branch was “lost”. Almost every “I lost my work” ends here — it’s the undo button for the whole repository. more in the glossary → → hash → git branch <name> <hash>
“The Merge — Combining the changes from one branch into another. A merge brings in every commit from the source branch — other people’s too — so first you check what exactly comes in. more in the glossary → went wrong” (before pushing)The pre-merge position is recordedgit reset --hard ORIG_HEAD
“I need the file as it was on Tuesday”History keeps every versiongit show <commit>:<path> · git restore --source=<commit> -- <path>

git reflog is the undo button for the whole repository: it lists every position HEAD has been at, newest first, with an identifier next to each. Almost every “I lost my work” ends here — you find the moment before the mistake and put a branch on it. Learn this one command and the rest stops being scary.

Two habits to finish. Looking at the past changes nothing: git checkout <deploy-tag> shows exactly what was in production back then, and git switch - takes you back. Before anything that frightens you, make a save point: git stash (set uncommitted work aside) or a plain commit on a scratch branch. Destructive git commands have no Dry-run — A “dry” run — the script shows what it WOULD do but changes nothing. You see the effects before executing; a data change happens only after deliberate confirmation. more in the glossary → mode — committing first is the dry-run.

Tasks as issues

One task = one issue ↗

This doctrine is written for one developer with AI — but “solo” no longer means “one writer”. A fix in one session, a new feature in another, a review in a third: that’s a multi-writer situation, and the “other side” is usually another agent session, not a person. So you need what a team needs: a shared task list.

An issue is where the why lives — not in your head and not in a chat that will disappear. The title describes the outcome (“Cart loses items after refresh”), the body gives the context and acceptance criteria. For anything non-trivial the issue comes before the code, so the scope and the decision are written down before a Diff — A list of the differences between two versions: what was added and removed, line by line. Reviewing the diff before merging is the last chance to see what’s really going to production. more in the glossary → appears. Small, specific issues beat one “epic for everything”. The task list is a list, not a memory: labels (bug/feat/chore, priority), closing what’s stale. An issue dead for weeks is a decision to make — do it or drop it — not a zombie.

Short branches, small changes

Short branches ↗

main is always deployable — it’s the shared table holding what goes to production. Each task gets a branch (feat/12-cart) that lives hours or days, not weeks: the longer it lives, the more painful the merge. A small PR can be reviewed in fifteen minutes; a huge one nobody reads carefully — it passes “on trust”, which means without review. After the merge the branch is deleted and the next one starts from a fresh main. Syncing with main often gives small, frequent conflicts instead of one gigantic one at the end.

Know what a merge carries

Diff before merging into the deploy branch ↗

A merge brings in every commit from the source branch, not just yours — including work other sessions have already merged there. A blind merge of a shared branch can carry an unfinished feature to production, riding along behind your urgent fix. So before merging into the deploy branch: git log <deploy>..<source> --oneline and read that list. An urgent, isolated fix is moved by Cherry-pick — Copying one chosen commit from one branch to another, without the rest of the changes. It ships an urgent fix without the unfinished work next to it — but check the result, as it can pull in neighbouring hunks. more in the glossary → that one commit onto a clean deploy branch, instead of merging a whole integration branch nobody has checked. It’s the same principle as with orphans: don’t ship what you didn’t mean to.

A cherry-pick needs checking too

After any cherry-pick, verify the result: ↗

A cherry-pick between diverged branches isn’t clean. When the target branch lacks context the commit’s parent had, git falls back to a three-way merge and can pull in neighbouring hunks from the source branch. In the reference project that’s how production got a Hook — Automation in the tool’s configuration that always fires on a specific event (e.g. after a file edit). Anything that must happen “always after something” shouldn’t depend on the assistant remembering. more in the glossary →-up for a file that didn’t exist on the deploy branch — require("./routes/feed") — and the app crashed at startup with MODULE_NOT_FOUND, while users got a 502 error.

So after every cherry-pick: git diff <deploy-base> HEAD --stat and read the changes wherever the app wires up modules. The result should show only your change. “10 files, +320 lines” with one stray app.use(require(...)) for a three-line fix is an alarm signal.

Every session in its own tree

One concurrent session = one git worktree — A separate working copy of the same repository, so a second parallel session works in its own folder and doesn’t clash with the first. Two sessions in one folder overwrite each other’s work; a separate working tree prevents it. more in the glossary → ↗

Two sessions in one repository folder share one git state: they stage files for each other, sweep in half-done changes, and the symptoms look like ghosts (a branch nobody switched to; someone else’s files in git status; edits that “disappear”). A separate git worktree gives each session its own working copy — at the cost of one more directory (16).

Branching that grows with the project

Branching grows with the project — add each rung when a metric forces it: ↗

You don’t start at the top. Three rungs:

  1. Solo, no live users → just main. Commit directly, with discipline. No branches, no ceremony — the simplest thing that works (12: don’t overengineer).
  2. First production deploy (even solo) → short branches into main. Now main is “what’s live”, so it must always be deployable: every change on a feat//fix/ branch, a diff review, a merge, a tagged deploy.
  3. More people or parallel streams → develop + preprod. Feature branches merge into develop (integration), a release goes develop → main, and before production it’s checked on preprod — a copy of production (18).

The triggers are concrete: live users → rung 2; a second person or parallel work → rung 3. A three-environment process for a solo prototype is wasted time; a bare main that real users depend on is asking for an outage.

Review isn’t a formality but a second pair of eyes before production. In a team every PR has a reviewer; solo, the reviewer is a deliberate second pass over the diff — and Claude playing devil’s advocate.

Deploy only on the word

Never deploy to prod automatically. ↗

A pull on the server, a process reload, a Migration (database) — A controlled change to the database layout — adding a column, a table or moving data — step by step. Like a renovation to plan: rebuilding data in a set order so nothing “collapses”. more in the glossary → on production, a database swap, switching on the Maintenance window — A short, planned period when users see a “work in progress” page while the database or configuration changes behind it. It guarantees the app never runs, even briefly, on half-changed data. more in the glossary → — each of these touches real users. The assistant can propose a deploy when everything is ready, but it waits for an explicit “yes”. This rule beats any automatic mode: autopilot works within boundaries, and a deploy is outside them.

The server only sees origin

The server deploys origin/<branch>, not your laptop. ↗

The deploy script pulls on the server whatever is in the remote repository. It never sees the working copy on your computer. Work that is committed but not Push / pull — Push sends your commits to the remote repository; pull fetches changes from there into your copy. A push is an “outward” action — in The Craft it needs explicit consent, like a deploy. more in the glossary → simply doesn’t ship — and it’s most dangerous when it was a parallel session that committed but didn’t push. You deploy, the server pulls an older version and the “latest changes” are silently missing: a renamed button still shows the old text, a new feature isn’t there, and locally everything looks fine.

The check goes both ways: git log origin/<branch>..<branch> shows commits not yet on the server (push first), and git log <branch>..origin/<branch> shows the opposite — another session pushed something newer and you’re about to deploy a stale tree (pull first). In one sentence: reconcile local with origin before a deploy, because the server sees only origin.

Check the startup before you switch

Boot-check on the target before reloading: ↗

An error while loading modules (a missing file, a bad import) only shows up when the process starts. A reload (e.g. pm2 reload), meanwhile, starts the new code while killing the old — so the failure lands on users as a 502. The fix: after the pull you run the new code in a one-off process (node -e "require('./src/app')", with app.listen hidden behind require.main === module so it doesn’t take the port) and reload only when it prints your “OK” marker. If it throws, the running process stays untouched — production stays on the old code and you fix things calmly.

Important: this is done on the target server (real node_modules and environment variables), not on a laptop. A working copy without installed dependencies gives a false “it starts”.

Two kinds of deploy

Two deploy types. ↗

A code-only change goes out with no downtime: pull, boot-check, reload. A change with a migration, new dependencies or a database swap goes out in a maintenance window: a flag in nginx — A program at the front of the server that takes traffic from the internet and routes it to your app. Handles HTTPS, serves static files, balances traffic — the server’s proven “reception desk”. more in the glossary → switches traffic to a branded page with a 503 code, a few seconds of downtime — and the guarantee that the app never runs, even for a moment, on a half-migrated database schema.

Several projects can share one VPS — A rented piece of a cloud server “just for you”, where you put an app “live”. Predictable cost and full control. One VPS easily carries a few small projects. more in the glossary →: static sites get there via rsync and a separate nginx vhost, Node apps via git pull and a process manager behind nginx. One server means a shared Backup — A copy of your data saved earlier, which you return to when something goes wrong. It’s the only way to undo a change to data — code comes back from git, data only from a backup. more in the glossary → directory and the same habits for every project; a new project gets its own vhost and directory.

A tag on every deploy

Tag every deploy: ↗

git tag -a deploy-$(date +%F) -m "What goes to production: <one sentence>"   # -2, -3 for another one that day
git push --tags

What’s in production right now? git describe --tags --abbrev=0 --match 'deploy-*'. Rolling back code means checking out the tag and reloading; rolling back the database schema means restoring a backup — because migrations only go forward. Without tags, “what’s live?” becomes guesswork.

A deploy script has invariants worth guarding with a test too. A real-life example: $(date) expanded once, when the script was created, instead of on every run — every backup overwrote the same file, and for a long time nobody noticed.

A changelog with every deploy

Every deploy updates the public changelog ↗

The public “What’s new” is written in plain language, without jargon — no “Scraping — Automatically gathering data from websites with a program instead of copying by hand. Powerful for acquiring data, but it needs manners: respect others’ rules (robots.txt), don’t overload the server. more in the glossary →”, “migration”, “commit”. What matters is what the user gained. Updating the changelog (together with its date) is an item on the deploy checklist, not something for a free moment.

The runbook as incident memory

Every prod incident becomes a dated, numbered lesson in the Runbook — A written step-by-step guide for production operations, with numbered lessons from incidents. The memory of outages can’t live in someone’s head — the next deploy starts by reading the runbook. more in the glossary → ↗

The memory of outages can’t live in someone’s head, because heads (and conversations with an assistant) end. Every outage → an entry with a date and a lesson number, e.g. “never copy a live SQLite — The simplest database — the whole thing lives in one file, with no separate server. A great default to start: zero configuration, easy backup (you copy the file). As you grow, you migrate up. more in the glossary → database with scp — use .backup; WAL — Write-Ahead Log: a database mode where changes go to a journal first, then to the data. Gives safety (recovery after a crash) and better read/write concurrency. more in the glossary → mode keeps the freshest changes outside the main file”. The next deploy starts by reading the runbook, not by repeating the same mistake (14).

The first day of real traffic

First real users = a nightly database backup, deploy tags and a branded maintenance page — all on day one of traffic. ↗

These three things cost an hour when done up front, and a night when they have to be added in the middle of an outage. The critical path — sign-up, login, logout, whether the session survives a restart — is deployed with a test and a full Smoke test — A quick “does it even work” test right after a deploy — checks the most important paths (e.g. login). Catches disasters in 30 seconds before a user sees them. A cheap way to sleep well after a deploy. more in the glossary → (03).