Programming with AI
ENRU
5.1

Git as insurance

a commit before every task

What it is

Git is a system that keeps a history of changes. For working with an AI it matters more than for working by hand, for one reason: it makes rolling back cheap.

A rollback is the main tool from article 4.10, and its price depends entirely on when the last commit was. A commit an hour ago — the rollback costs an hour. A commit before the task — the rollback costs nothing.

What it looks like in code

Diagram from the book — to be redrawn for the web
A commit before every task. A failed attempt rolls back to the nearest point, not to this morning.

Four commands are enough

git status --short          # what changed
git add -A                  # take all the changes
git commit -m "description" # record them
git log --oneline           # look at the history

The output of git status --short reads simply:

measured
 M bot.py
?? temp.txt

M — the file was modified, ?? — the file is new and git doesn't know about it.

The habit this is all for

A commit before giving the AI a task. Before, not after.

git add -A && git commit -m "before adding the /stats command"

Five seconds. After that, whatever happens — however many files the AI rewrites, whatever it breaks — getting back to a working state costs one command.

Rolling back

git checkout -- bot.py      # roll back one file
git checkout -- .           # roll back everything uncommitted

Verified on a live repository: a file containing print('broke it') became print('v2 — added weather') again after git checkout -- bot.py.

If throwing it away feels wrong:

git stash                   # set it aside
git stash pop               # bring it back

Going back to an older commit:

git log --oneline           # find the one you want
git reset --hard 7c93d98    # return to it

What to put in a commit message

Short, and about why rather than what. The what is visible in the diff.

BadGood
fixfixed KeyError when the city is missing
update bot.pysubscriptions moved to SQLite
changesbefore refactoring the handlers

The last example is that "before the task" commit. A week later, a message like that tells you where the point of return was.

What to tell the AI

Prompt
I've committed the current state. Go ahead and make the change;
if anything goes wrong I'll roll it back.

The sentence is not for the AI but for you: it checks that the commit really was made.

A useful use of git in conversation with an AI:

Prompt
Here is the diff of my changes this session. Write a commit
message: one line, about why it was changed.
Prompt
The code worked yesterday and broke today. Here is the git log
for two days. Which commit should the search start from?

What to look for

Commits once a day. Then a rollback costs a day. Commit by task, not by the clock.

Everything in one commit. A refactoring, a feature and a fix together — and now none of them can be rolled back separately (article 3.6).

git add -A without looking at status. That is how .env, databases and temporary files get into a commit. status first, add second.

No .gitignore. Then .venv with all its libraries, and your secrets, go into the repository. That is article 5.7.

A "just in case" commit instead of a rollback. Committing something broken so as "not to lose it" leads to a history with no working point in it.

Cheat sheet

  • A commit before the task, not after. Five seconds.

  • Four commands: status --short · add -A · commit -m · log --oneline.

  • Roll back: checkout -- ., set aside: stash, return: reset --hard.

  • reset --hard deletes uncommitted work irreversibly.

  • Write why, not what.

  • One task, one commit.