05 — Git and deployments
Commandments II, VI, VIII, IX. Git is the project’s memory and the undo button for everything else; the 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 → policy is where a mistake costs the most. 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 → → 18; 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 → 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 → → 19.
Git habits
- Search the history before you implement:
git log -S"symbol" --oneline # where a symbol/flag/column was born git log -G"regex" --oneline # commits whose diff matches git log --grep="word" --oneline # commit messages git blame <file> -L a,b # who/when/why - Keep
git statusclean. One topic per 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 →. Junk (.bak, temp files, stray databases) goes to.gitignoreearly (#comments on their own line). A file shown as changed with an emptygit diff HEADis 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 → noise, not work — don’t commit it. - Orphans: uncommitted work you didn’t write (another session’s) is never swept into your commit or deploy without confirmation — ask whether it’s finished work or an experiment.
- Commit messages in English, what + why,
refs #N/fixes #N.
Undo: you probably didn’t lose it
Committed work is recoverable; git remembers every position a 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 → held (~90 days). Commit early — a commit is free, private until 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 →, and is your save point.
| Situation | Way back |
|---|---|
| Wrecked a file, not committed | git restore <file> |
| Undo the last commit, keep the work | git reset --soft HEAD~1 |
| Committed on the wrong branch | git branch <right> here, then git reset --hard HEAD~1 on the wrong one |
| Deleted a branch / “lost” commits | Reflog — 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 → → find the hash → git branch <name> <hash> |
| A 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 (not pushed) | git reset --hard ORIG_HEAD |
| Need a file as it was | git show <commit>:<path> · git restore --source=<commit> -- <path> |
Only three commands destroy work — and only uncommitted work: git reset --hard, git restore . /
git checkout ., git clean -fd. Git has 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 → for them: committing first is the dry-run (→ IV).
Parallel work: issues and short branches
“Solo” is still multi-writer: one developer routinely runs several Claude sessions 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 →. The “other party” is usually another session — read “teammate” as “the other session”.
- One task = one 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 →: title = outcome, body = context + acceptance criteria. The issue number ties branch, commits and 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 → together; the PR/merge closes it, not a work-branch commit.
- Short branches (
feat/NN-cart,fix/NN-login) living hours–days; small PRs; merge, delete, sync withmainoften. - 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 → before merging into the deploy branch — a merge ships every commit on the source, including
other sessions’ work:
git log <deploy>..<source> --oneline. An urgent isolated fix is 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 →. - After any cherry-pick, verify the result:
git diff <deploy-base> HEAD --statmust show only your change — a 3-way fallback can drag in a neighbour’s hunk (a strayrequire→ boot crash → 502). - 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 →, never two sessions in one checkout (→ 16).
Branching grows with the project — add each rung when a metric forces it:
- Solo, no live users → just
main, committed to with discipline. - First production deploy → every change on a short branch, reviewed, merged to an always-deployable
main, deploy tagged. - More people / parallel streams →
developfor integration, releasesdevelop → main, verified on a preprod copy of prod first (→ 18).
Every change reaches main reviewed — solo, the reviewer is a deliberate second pass over the diff.
Deployments — the overriding policy
⛔ Never deploy to prod automatically. Pull on the server, reload, 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 →, database swap, 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 → flag — only on the user’s explicit “deploy”. You may propose a deploy; you wait for the yes. Commit and push are not a deploy. This rule beats any auto mode.
- The server deploys
origin/<branch>, not your laptop. Before every deploy:git fetch, thengit log origin/<b>..<b>(unpushed commits → push first) andgit log <b>..origin/<b>(another session pushed → pull first). - Boot-check on the target before reloading: after the pull, start the new code in a throwaway process
(
node -e "require('./src/app')", withlistenbehindrequire.main === module); reload only on its OK marker. A crash then leaves the old process serving. - Two deploy types. Code-only → pull + boot-check + zero-downtime reload. Migration / dependency change / database swap → a maintenance window (branded 503 page) so nothing runs on a half-migrated schema.
- Tag every deploy:
git tag -a deploy-$(date +%F) -m "<what goes live>"+git push --tags. What’s live:git describe --tags --abbrev=0 --match 'deploy-*'. Code Rollback — Reverting a change to the previous, working state — “Ctrl+Z” for a deployment. When a new version breaks production, a rollback restores the previous one in seconds instead of fixing in a panic. more in the glossary → = checkout the tag + reload; schema rollback = restore the 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 →. - Every deploy updates the public changelog in plain language — what the user gains.
- 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 → (
docs/), read before the next deploy (→ 14). - First real users = a nightly database backup, deploy tags and a branded maintenance page — all on day one of traffic.
Anti-patterns
- 🚫 Auto-deploy, “it’s ready so it goes”.
- 🚫 Deploying without reconciling local ⇄
origin— unpushed commits silently don’t ship. - 🚫 Merging a shared branch into the deploy branch without reading what it brings.
- 🚫 A long-lived “big refactor” branch; an “everything at once” PR.
- 🚫 Working without an issue — the “why” is gone a month later.
- 🚫 No deploy tag — “what’s live?” becomes guesswork.
- 🚫 A deploy-script invariant with no test (
$(date)expanded once → every backup overwrites the same file).