1. Set up a vault and make the first checkpoint
Oliver pulls Tommy's laptop toward himself, opens a terminal in the portal folder. His fingers fly across the keys with confidence — you can tell right away he has done this a thousand times.
— So. You get the model: main, branch, commit, rollback. Now — every word has its own command. Watch and write it down; I am showing this only once.
— First. Set up git in the project. Once per project, right at the start.
$ git init
Initialized empty Git repository in /portal/.git/
— That is it, — Oliver says. — A hidden folder called .git appeared, and that is where git stores the whole history. Now it is keeping track of the project. Next — before changing anything, you need to take a look around. The command I hit a hundred times a day:
$ git status
On branch main
No commits yet
Untracked files:
pages/
src/reward-flow.js
src/leaderboard.js
…
— status means “where am I and what is around me,” — Oliver explains. — Right now it says: branch main, no commits yet, and these are files that git can see but has not taken under supervision yet — “untracked,” not tracked. Remember that word; we will come back to it. Get used to hitting status all the time: glance before every step — where are you standing?
— Now we set the first restore point — a clean portal, before any edits. That takes two moves. First, tell git which files to put under control with the add command:
$ git add -A
— -A means “everything in the current folder.” It is like putting the files on the table in front of the photographer. And now — the shot itself, the commit:
$ git commit -m "working portal before changes"
[main (root-commit) a1b2c3d] working portal before changes
3 files changed, 214 insertions(+)
Tommy points at the line:
— Why is it in quotes?
— The message attached to the commit point. — Oliver nods. — So that a week later you can understand what moment this is. “Working portal before changes” — and I can see right away: you can fall back here, everything is intact. Without a message, a commit is like a photo without a date — you end up guessing later.
2. Branch off and save the work
— Next in the model is a separate line for the task. We do not touch main; we branch off:
$ git checkout -b fix-reward
Switched to a new branch 'fix-reward'
— “Switched to a new branch fix-reward,” — Oliver reads aloud. — The -b flag means “create it and switch to it immediately.” Now I am standing on the new branch, main is off to the side. I will hit status to check:
$ git status
On branch fix-reward
nothing to commit, working tree clean
— See — “on branch fix-reward, clean.” That is where you work. You made changes, tried them, and when you are happy, you save a new point. The same two moves, add and commit:
$ git add -A
$ git commit -m "fix: reward accrual in reward-flow"
[fix-reward e4f5g6h] fix: reward accrual in reward-flow
1 file changed, 3 insertions(+), 1 deletion(-)
3. See what changed and roll back if it is broken
— Before you accept anything — look at what changed. The diff command:
$ git diff
reward-flow.js
+ const reward = base * multiplier;
- const reward = base;
— Green is what you added, red is what you removed, — Oliver points out. — Before you commit, look it over with your own eyes: did it change the way you wanted?
— And if I added extra stuff? — Tommy is already getting it.
— Then — rollback. Return the branch to the last saved point. Here is the command, but be careful with it; it has teeth:
$ git reset --hard HEAD
HEAD is now at a1b2c3d working portal before changes
— reset --hard wipes out everything added after the checkpoint and restores the files as they were, — Oliver says with a serious face. — HEAD is “the last committed point where I am standing right now.” Rolled back — and it is gone. A powerful thing, so remember: --hard does not ask; it wipes silently. Think twice before pressing it.
— And if you want to see all your points at once — here:
$ git log --oneline
e4f5g6h fix: reward accrual in reward-flow
a1b2c3d working portal before changes
— log is the stream of your commits, with the newest at the top. The whole history of the project is right in front of you: I can go back here, and here. Remember how you sifted through ten final folders with your eyes? That is it, only honest and in one line.
Tommy has filled half a notebook. init, status, add, commit, checkout -b, diff, reset --hard, log — eight commands, and each one has those flags too, -A, -m, -b, --hard, --oneline. His head is already buzzing.
— Uncle Oliver, do we really have to know all this by heart? — he asks cautiously.
— That is how we were taught — by heart, — Oliver shrugs and takes the mug. — Your fingers should remember on their own. I have been typing these commands from memory for twenty years. Well, do as you like — you young folks have your own tricks. My job is to show how it really works. — And, satisfied, he walks back to his place.
Tommy looks at the notebook covered in writing, and the excitement of “I can use git now” quickly gives way to sadness. Eight commands. Flags. He is already mixing up half of them: -A is where, --hard is what wipes out... With this gloomy notebook, he turns toward the chat with Alex.
GO TO FULL VERSION