Vibe coding · Git basics · For non-programmers
AI Broke My Working Code: How to Go Back With Git
It worked ten minutes ago. Here is the undo button nobody told you about.
Three commands, one habit, and one honest warning about what git cannot save.
You asked the AI for one small change. A button color, maybe. It said “Done!” in that cheerful voice, you refreshed the page, and now the whole thing is a white screen with a red error.
Your stomach drops, because you remember the app worked a minute ago, and you can’t remember what it looked like inside. You scroll up. You ask the AI to fix it. It apologizes, changes four more files, and now it’s worse.
This is the moment version control exists for. Git is an undo button for a whole project, but only for the moments you told it to remember. The rest of this post is about pressing that button correctly.
The short version
If you saved (committed) your project to git before the AI changed it, run git status to see what changed. Then use git restore <file> to drop unsaved edits, or git revert HEAD to cancel a saved change. If you never committed, git has nothing to go back to.
⚠️ What this post is, and isn’t
I ran every command below in a throwaway test project on Nov 10, 2026 (git 2.43.0, Linux) and copied the real output. I have not tested them inside each AI tool, and this is a how-to, not a diary of a disaster. Commands are checked against the official git documentation.
What git actually is (the 60-second version)
Think of a video game with save slots. Each time you run git commit, you make a save slot. The AI can wreck your files afterwards, but the slot is still there. A commit is a save point, and git can only take you back to a save point that exists.
That one idea explains everything else, including the sad part at the end of this post. If you have never installed git, you can skip ahead to the habit section and come back.
How to undo AI changes before you commit
Start with the safest command in this post. It changes nothing. It just tells you what the AI touched:
git status
git diffgit status lists changed files. git diff shows the exact lines: a minus for what was removed and a plus for what was added. In my test, the AI “fix” turned price * qty into price + qty. A one-character change that silently breaks a checkout total.
Undo one file: git restore
git restore cart.pyPer the git restore documentation, this copies the file back from your last saved state. It overwrites your current edits to that file, and there is no undo for that step. In my test, git status printed nothing afterwards, which means clean.
How to undo a commit the AI already made
Some tools commit for you. Or you committed in a hurry, and only then noticed the damage. For that, use:
git revert HEADThe git revert documentation describes it as recording a new commit that reverses an earlier one. Your history stays intact, which is why it is the gentler option. In my test the log afterwards read: the revert on top, then the broken commit, then the good one underneath.
What if I went back too far? (git reflog)
People reach for git reset --hard because it sounds decisive. It moves your project back to an earlier save point and wipes uncommitted edits. In my test, a line I added and never committed was gone after git reset --hard, and the reflog did not bring it back.
The commit you stepped over is a different story. The git reflog documentation says reflogs record where your branch tips have been, and by default entries are kept for 90 days if reachable and 30 days if not. Run git reflog and you can usually find the commit you thought you lost. In my test it listed every step, including the commit I had “lost”.
The habit that actually makes this work
Every undo above depends on a save point from before the AI ran. So before you start an AI session, or before any big request, run two lines:
git add -A
git commit -m "working version before AI change"That is the whole habit. Thirty seconds of saving buys you the ability to shrug when the AI breaks things. Plain-language rule: if it works, commit it.
💡 A small test worth doing once
Make a throwaway folder, run git init, commit a file, edit it badly, and practice git restore. Doing it once when nothing is at stake is how it stops feeling scary at 11pm.
The honest limitations
Git cannot save what you never committed. If the AI overwrote a file you had not saved to git, there is no hidden copy for git to give back. Your editor’s local history might help, but that is a different tool and I have not tested it here.
git restore will not delete new files the AI created. In my test, a brand-new untracked file stayed in the folder after the restore. Check git status for lines starting with ??.
Reflog is local. It lives on your computer, and its entries expire. It is a short-term safety net, not a backup. If the project matters, push it to GitHub too.
I did not test every AI tool. These are plain git commands, so they should behave the same wherever git is installed. But how a particular tool handles commits is up to that tool.
Who should actually care
Worth it if: you build with AI and have ever said “it worked a minute ago”.
Maybe not if: you only make single-page experiments you would happily regenerate from scratch.
Skip if: your tool already keeps full version history for you and you are comfortable with it.
Quick answers
Do I need to learn all of git?
No. For this problem you need status, diff, add, commit, restore and revert. Ignore the rest for now.
Can I undo git restore?
No. It overwrites your unsaved edits to that file, so look at git diff first if you are unsure.
How long does reflog keep my history?
By default 90 days for reachable entries and 30 days for unreachable ones, per the official reflog page. Those defaults can be changed in settings.
The bottom line
AI will break working code sometimes. That is not a reason to stop building, it is a reason to save often. Git turns a disaster into a ten-second fix, as long as you saved first.
Do this today: open your current project, run git status, and make one commit named “working version”.
Written Nov 10, 2026. Updates will be logged here: none yet.