HOYALABA PERSONAL RESEARCH NOTEBOOK

Engineering Notes / 2026-08-31 / 8 MIN

Commit before you ask: Git as a safety net for AI coding

AI changes code boldly and sometimes ruthlessly. Part 2 sets up the safety net: commits as save points, rolling back on purpose, short-lived branches, one commit per request, and the difference between Git and GitHub.

UPDATED / 2026-10-20

The series cover: a staircase of colored steps with a person standing at the top, one step for each lesson.
FIG. 1 · The series cover: a staircase of colored steps with a person standing at the top, one step for each lesson.

Last time, we covered the stage the AI works on: paths, the terminal, reading Python, and virtual environments. I ended with a warning: AI changes code boldly, and sometimes ruthlessly. Today is about surviving that. We're building a safety net.

Commits are save points

Let's start with the intuition. Here's how I put it in class:

Vibe coding without commits is like playing a hard game without saving.

Someone always asks, "Doesn't Ctrl+S already save?" Good question, but they're completely different things. Ctrl+S keeps only the latest state. Git lets you return to states you committed: yesterday afternoon, or last week.

That makes a commit message a note to your future self. A good message is one that tells you at a glance, months later, what that commit was.

Save points aren't the whole story, though. Git also gives you a structure that lets many people work on the same code together. Real team collaboration is beyond where we are, so today we'll look at Git purely as a way to make save points.

Git flow: git add stages files, git commit records a local save point, and git push sends commits to the remote. git restore . copies the staging area back to tracked files, discarding unstaged edits.
FIG. 2 · Git flow: git add stages files, git commit records a local save point, and git push sends commits to the remote. git restore . copies the staging area back to tracked files, discarding unstaged edits.

GPT drew this to help us see the flow. There's no shortage of Git tutorials out there, so I won't link any in particular.

1. Today's rule

If you take away one sentence, make it this one:

2. The three steps of Git

Now for how it actually works. Git moves in three steps:

  • Working folder: you edit the code.
  • git add: you put what you want to save onto the stage.
  • git commit: you record it as a save point.

Before the first git add, create a .gitignore listing files and folders that should stay out of Git:

.env
venv/
__pycache__/

Why list .env before it even exists? In part 5 it will hold secrets like API keys, and if you plan to add the line "later," you'll forget, and that's how accidents happen. It's safer to have it there already. File browsers that hide dotfiles may not show it, so check for it in your editor or terminal. A .gitignore rule doesn't stop Git from tracking files that were already added.

Then make your first save point:

git init                                     # start tracking this folder with Git
git add .                                    # put unignored files on the stage
git commit -m "Finish allowance calculator"   # save!

To check your work, git log --oneline lists your save points and git diff shows unstaged changes. Use git diff --staged to see changes you've staged for the next commit.

3. Exercise: break it on purpose, then roll back

You don't strictly have to go this far, but it's worth learning in your hands that you can go back.

# 1. Save the current state
git init && git add . && git commit -m "Part 1 result"
# 2. Ask the agent: "Rewrite this code in a completely different style."
# 3. See what changed
git diff
# 4. Discard unstaged edits to tracked files
git restore .

For this exercise, don't stage the AI's edits before restoring. git restore . copies tracked files from the staging area; it doesn't remove newly created, untracked files.

The tracked code comes right back. In class, this is where we all clap, because it's the moment you know, in your hands and not just your head, that "even if the AI wrecks it, I can undo it."

Why does that matter? Because that experience is what lets you experiment boldly next time. Once you know you can go back, the fear goes away. Without it, asking the AI to do anything starts to feel risky.

4. Branches divide the blast radius, not the features

Now branches. I explain them with a tree.

main is the trunk. Each time you build a feature, you grow a branch. If it works out, you graft it onto the trunk (merge); if it fails, you cut off the whole branch.

A lot of people take branches to mean "a way to split up features." That's not it:

A scenario makes it obvious. Say you built a shift-schedule page, then a lookup page, then a login page, all on main, one after the other. Now you find that the schedule page, the very first thing you built, was broken, and everything stacked on top of it misbehaves too.

Rolling everything back would also remove the later work. You can often fix or revert only the faulty commit, but later code may depend on it. Separate branches let you test a change before merging it; they don't automatically remove those dependencies.

5. The trap beginners fall into

But wait. Almost everyone who learns branches falls into the same trap. I did too.

You think, "I'll merge it when the feature is finished." Then the branch lives for days, or weeks.

Large projects often adopt a strategy called Git Flow, with separate branches such as feature, develop, hotfix, and main. But when you're developing alone and keep building on one branch for a long time, it can get hard to merge back into main later.

So rather than living on one branch, it's better to merge as soon as a piece is done. Plenty of other developers have written about running into exactly this problem; here's one post comparing Git Flow with trunk-based development (in Korean).

The longer a branch lives, the further it drifts from the trunk, and the harder it becomes to resolve conflicts. That's called merge hell, and trunk-based development, the approach used at large companies like Google, exists to avoid it.

An agent can change several files before you've had time to review them. If separate branches touch the same code, bringing them back together becomes harder. Keeping each change small makes that easier to manage.

So I set these rules:

  • Default: merge a branch into main within a day.
  • When to merge: not when the feature is done, but when nothing is broken.
  • If it's going to take longer: don't add branches; split the feature.
  • Mark safe points: git tag v1-working means "everything up to here definitely works."
  • Undo: git restore discards unstaged edits to tracked files; git revert <commit> records a new commit that reverses a committed change, useful for shared history.

Personally, I think the third rule is the key. Instead of keeping a branch alive, cut the work smaller. It's exactly the same principle as "ask for small pieces," which comes next time.

The commands themselves are simple:

git switch -c feature/greeting    # grow a branch
# ...do the work...
git add . && git commit -m "..."
git switch main                   # back to the trunk
git merge feature/greeting        # graft the branch on
git branch -d feature/greeting    # tidy up the finished branch

6. A new commit rule for the AI era

This is the part I personally find most interesting. When people wrote all the code by hand, committing per feature came naturally. But an agent changes several files at once.

So the rule changes:

That buys you two things. Your history becomes a record of what you asked the AI to do, and the unit you roll back matches the unit of work.

It doesn't have to be this strict every time. Usually we give the AI one feature at a time and then do a bit of debugging, so the idea is to commit after each round of request plus fixes, the way you'd save a game.

One more habit worth adding: note in the commit message whether you checked the result.

✗ "Fixed stuff"
✓ "Add greeting feature (ran it myself and checked)"

Six months later, git log tells you which commits were verified. It seems minor, but it really pays off later.

7. Git and GitHub are different things

One last point. A surprising number of people think these are the same. They aren't.

  • Git is a tool: the save feature itself, running on your computer. It works without the internet.
  • GitHub is a service: a website that stores your code in the cloud.

If Git is Ctrl+S, GitHub is Google Drive. GitHub gives you three things: a cloud backup, a portfolio, and the starting point for deployment in part 5.

Some people always get stuck on authentication when they git push. If that happens, the GitHub Desktop app lets you push with a few clicks, so you don't have to wrestle with the terminal. Still, I recommend installing Git and using it from the terminal.

Also, installing an extension like Git Graph in your editor gives you an at-a-glance view of your history, which is handy.

Today's summary

  • Safety rule #1: always commit before asking the AI for a big change.
  • Recovery: git restore . discards unstaged edits to tracked files.
  • .gitignore: keeps out what must never be uploaded (.env comes in part 5).
  • Branches: divide the blast radius, and keep them short, under a day.
  • Commit unit: one AI request, one commit.
  • GitHub: cloud backup, portfolio, and the starting point for deployment.

Now that you have a way to recover your committed work, you can give bold instructions and fix things freely. Next time, we learn how to hand work to an AI properly, and how to review and approve its plan before any code is written (plan mode).

Sources