Git for Vibe Coders: Five Commands and a Safety Net

Git is a history of your project that your AI tool cannot quietly edit away. Five commands cover almost everything you will do with it: git status, git commit, git switch -c, git log --oneline and git restore. The safety net is two habits: commit before you let an agent loose, and push to GitHub so the history lives somewhere other than your laptop.
You may already have an undo button in your tool. That is not the same thing, and the gap is where projects get lost.
Why not just use the undo button?
Claude Code’s /rewind restores the files its own editing tools changed. A file that only a shell command touched is not tracked, so an rm or a sed -i on a file Claude never edited is outside the safety net. Claude Code’s own docs say to use Git for that. Its snapshots are also swept after about 30 days by default (Claude Code docs).
Cursor’s checkpoints are snapshots of the files its agent modified, stored locally and separate from Git. Cursor’s help page says to use Git for permanent version control (Cursor docs).
And sometimes the tooling itself surprises you. In March 2026 one Cursor user reported that a 2.5-hour agent session, 25+ modified files, silently ended with the working directory reset to clean. By their account the changes had been moved into a git stash, and they only recovered them because they found the stash before it was garbage-collected (forum report). That is one user’s report, not a confirmed bug with a known cause. The point is smaller and firmer: anything uncommitted is the only thing at risk. Committed work is much harder to lose.
Set up before the first commit
Create a .gitignore that lists .env before you run git add for the first time. Here is what happens without it, in a scratch folder:
mkdir s2 && cd s2 && git initecho "SUPABASE_SECRET=x" > .envgit add -Agit status --short# A .env.env is staged and one commit away from your history. Adding .env to .gitignore afterwards does not help once it is committed, because git keeps tracking a file it already knows. git rm --cached .env stops tracking it, but the old commits still hold the value. If a key ever got committed, rotate it, as in how to find and rotate leaked keys.
The five commands, tested in a scratch repo
Every command below was run in a scratch repo (Git 2.34, Node 22; switch and restore need Git 2.23 or newer, so run git --version first, because some Macs ship an older one) with one function and one test, the kind of safety test a non-coder can ask for. To follow along, create package.json containing {"type":"module"}, then these two files:
export function total(cents, qty) { return cents * qty; }import { test } from "node:test";import assert from "node:assert";import { total } from "./price.js";test("total multiplies price by quantity", () => assert.equal(total(500, 3), 1500));node --test runs the test with nothing to install. Notice what the test checks: what the function does for a customer (3 items at 500 cents cost 1500), not how it is written. A test tied to the code’s shape would pass while the behavior broke. A behavior test fails the moment any later fix changes what users get.
1. git status shows what changed and what is new. Run it before every commit. If a file shows up that you do not recognize, stop and find out why.
2. git add -A && git commit -m "Working checkout total" is a save point. Commit when the app works, not when it is finished.
Now the agent “improves” the function and drops a stray file:
git status --short# M price.js# ?? notes.txtnode --test# fail 1git diff# -export function total(cents, qty) { return cents * qty; }# +export function total(cents, qty) { return cents + qty; }The test fails first, and that is the regression catching a break. The multiply became a plus.
3. git restore price.js throws away the uncommitted edit:
git restore price.jsnode --test# pass 1, fail 0Back to green. Two things the tutorials skip, both checked in the scratch repo: git restore . does not delete new untracked files (notes.txt stayed until it was removed by hand), and an uncommitted edit that you restore is gone for good. There is no undo for the undo.
4. git log --oneline lists your save points, newest first. Each short code is an ID you can go back to.
5. git switch -c try-discount starts a branch, a separate line of work for an experiment. It carries your uncommitted edits with it, which surprises people. Commit first, then branch. If the experiment is bad, git switch main and you are back, with the branch left behind to delete.
Bonus, the rescue for a file the agent deleted. Which command is right depends on what happened, and the wrong one silently gives you an older version. In the scratch repo, with v1 committed and then v2 committed:
- Deleted but not committed:
git restore price.jsbrings backv2, the last save point. Usinggit restore --source=HEAD~1 price.jshere brought backv1and quietly dropped the saved work. - Deleted and the agent committed the delete:
git restore --source=HEAD~1 price.jsbrings backv2. - Not sure which:
git log --oneline -- price.jslists the commits that touched the file, newest first. The top one is the delete, sogit restore --source=<top id>^ price.jsrestores the file as it was just before.
If work seems to have vanished without a delete, run git stash list first. Agents sometimes park changes there, as in the Cursor report above.
If you never use a terminal
You can do the same things with buttons. GitHub Desktop and the Source Control panel in VS Code show your changes, commit them and discard a file. In Lovable, the GitHub sync makes the commits for you, so the commit list on your GitHub repo is your save-point history. The habit is the same: look at what changed before you accept it.
The safety net
Commit before every agent session. A save point that exists before the agent starts is the only thing that a bad session cannot touch.
Push to GitHub. Your laptop dying is also a way to lose a project. A remote copy is backup and history in one.
Know the break-glass command. If you did git reset --hard and regret it, git reflog lists where your branch used to point:
git reflog# 08d3cd3 HEAD@{0}: reset: moving to HEAD~1# 7824bd9 HEAD@{1}: commit: Add discountgit reset --hard 7824bd9That brought the commit back in the scratch repo. Reflog entries expire after 90 days by default, and entries no longer reachable from your branch after 30 (git docs). That only helps for things you committed. Uncommitted work never reaches the reflog. For a stash you dropped, git fsck --lost-found lists unreachable commits, the last resort.
If you build in Lovable
Lovable’s GitHub sync is two-way but follows one branch at a time, by default main. If Lovable and GitHub have both added commits the other lacks, the next push from GitHub replaces Lovable’s version of the branch with the GitHub version. Lovable saves a backup first, and the project owner can restore it (Lovable docs). So pick one place to edit at a time, and look at the sync status before you push from your laptop.
A prompt to start with
Before you change anything, run
git statusand tell me if there are uncommitted changes. If there are, stop and ask me to commit. Work on a new branch. Never rungit reset --hard,git clean,git checkout .,git restore .,git stash drop,git stash clear,git branch -Dorgit push --forcewithout asking me first, and tell me what each would delete. After each change that works, show megit diff --stat.
The one check an engineer does by hand
Read git diff --stat before you commit. It lists every file the agent touched and how many lines moved. Engineers scan it for the file that should not be there: a deleted test, a changed config, a .env. A diff that touches 14 files for a one-line request is a diff to read, not accept. This habit is also what makes debugging with hypotheses possible, and it is the cheapest defense against an AI fix that breaks two things.
Once you commit after each prompt, git bisect can find the exact prompt that broke something. Git is also the base for the next layer of safety. Once you have it, staging environments and rollbacks stop being scary, and it is on the launch checklist for a reason. And when you start the next app, git works best under a real process: spec, slices, then prompts.
The Get AI Native ebook goes deeper on this. It is free, and the expected launch is around Christmas. Join the waitlist on the homepage.