Git and GitHub for Vibe Coders: The Safety Net Every AI-Built Project Needs

If you build apps with an AI coding agent, you have probably had this moment: everything worked an hour ago, you asked the agent for "one small change", and now the login page is blank, three files have been rewritten and you cannot remember what the working version looked like. Git is the tool that makes that moment boring instead of scary. It lets you save a snapshot of your project whenever it works, see exactly what the AI changed, and jump back in seconds.

This guide is for people who build with tools like Cursor, Claude Code, Windsurf, Replit or Lovable and have never really learned Git, or learned it once and forgot. You do not need to become a Git expert. You need about ten commands and a few habits. Everything below works the same on Windows, macOS and Linux, and costs nothing: Git is free and open source, and GitHub has a free plan for private repositories.

Why Git matters more when an AI writes the code

When you write code by hand, you change a few lines at a time and you remember what you did. An AI agent can change dozens of files in one go, including files you never opened. That speed is the whole point, but it also means:

Git gives you a complete history of every saved state of the project, and a precise diff (a line-by-line list of changes) between any two states. That turns the AI from a risky collaborator into a safe one.

The five ideas you actually need

Git has a reputation for being confusing, but day to day you only use five ideas:

  1. Repository (repo): your project folder, with Git tracking its history in a hidden .git folder.
  2. Commit: a saved snapshot of the project with a short message, like a save point in a game.
  3. Diff: the difference between two snapshots, or between your last commit and what is on disk right now.
  4. Branch: a separate line of work. You can try an idea on a branch and throw it away if it fails.
  5. Remote: a copy of the repo on another computer, usually GitHub, GitLab or Bitbucket. It is your backup and how you share code or deploy.

If you understand those five, the commands are just ways of using them.

Setting up once

Install Git from the official site (on macOS it may already be installed; run git --version in a terminal to check). Then tell Git who you are, once per computer:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Inside your project folder, start tracking it:

git init

Before your first commit, create a file called .gitignore in the project root. It lists files Git should never track. This step matters a lot for AI-built projects, because the most common serious mistake is committing secret keys. A minimal .gitignore for a JavaScript project looks like this:

node_modules/
.env
.env.local
.env.*.local
dist/
build/
.DS_Store

GitHub maintains a collection of ready-made .gitignore templates for most languages and frameworks, and the gitignore documentation explains the pattern rules. If your agent generated the project, open the .gitignore it created and check that .env files are listed. Ask the agent to add them if not.

The core loop: work, check, commit

This is the habit that does most of the work. Repeat it every time the app is in a working state:

git status          # what files changed?
git diff            # what exactly changed inside them?
git add -A          # stage all changes for the next commit
git commit -m "Add password reset email"

git status gives you a quick list. git diff shows the actual lines added and removed. Most AI coding editors also have a built-in diff view or a source-control panel, which is the same information with colours and buttons. Use whichever you prefer, but always look before you commit. You are checking for three things:

A good rule for AI-assisted work is commit after every working step, not at the end of the day. Small commits mean small rollbacks. If the agent just made the signup form work, commit. If it then breaks the dashboard, you lose minutes, not hours.

Writing commit messages that help you later

Commit messages are notes to your future self. "fix" and "changes" tell you nothing three weeks later. Write what changed and, if it is not obvious, why:

Some teams use a format called Conventional Commits with prefixes like feat: and fix:. It is optional for solo projects, but it is a handy habit. You can also ask your coding agent to write the commit message from the diff; just read it before accepting.

Undoing things: the commands that save you

This is why you are here. Here are the most common "undo" situations and the safest command for each.

"The agent made a mess and I have not committed yet"

To throw away all uncommitted changes and go back to your last commit:

git restore .

To throw away changes in one file only:

git restore path/to/file.js

New files the agent created are not removed by restore. List them with git status and delete the ones you do not want, or run git clean -n to preview and git clean -f to delete untracked files. Be careful: clean deletes files permanently.

"I committed something bad and want to undo that commit"

The safest way is git revert, which creates a new commit that reverses an old one, without rewriting history:

git log --oneline       # find the commit ID, e.g. a1b2c3d
git revert a1b2c3d

revert is safe even after you have pushed to GitHub, because it only adds history. The git revert documentation covers the details.

"I just want to look at the old version"

git log --oneline
git switch --detach a1b2c3d

This lets you run the old version to compare. Come back with git switch main (or master, depending on what your default branch is called).

"Something broke at some point and I do not know when"

If you have many small commits, Git can find the commit that introduced a bug for you with a binary search called git bisect. You mark one commit as good and one as bad, Git checks out a commit halfway between, you test and say good or bad, and it narrows down quickly. See the git bisect documentation. This only works well if you committed often, which is another reason for the small-commit habit.

Branches: let the AI experiment safely

Before asking an agent for a big change, such as "switch the database", "redesign the whole UI" or "refactor everything into TypeScript", create a branch:

git switch -c try-new-ui

Let the agent work there and commit as you go. If you like the result, merge it back:

git switch main
git merge try-new-ui

If you do not, just switch back to main and delete the branch with git branch -D try-new-ui. Your main branch never saw the experiment. Many developers keep main as "the version that works and could be deployed" and do everything else on branches.

Pushing to GitHub: backup and deployment

A repo that only lives on your laptop disappears with your laptop. Create a free account on GitHub (or GitLab or Bitbucket), create a new private repository, and follow the commands it shows you, which look like:

git remote add origin https://github.com/yourname/your-app.git
git push -u origin main

After that, git push uploads your new commits. Most hosting platforms for side projects can deploy straight from a GitHub repository, so every push to main can become a live deploy. If you go that route, the branch habit above becomes even more important: push experiments to branches, and only merge to main when they work. Our guide on how to deploy a vibe-coded app covers the hosting side, and free hosting for side projects compares options.

On GitHub you can also turn on branch protection for main, which prevents accidental force-pushes that overwrite history.

Secrets: the one mistake to never make

API keys for AI providers, payment gateways and databases cost real money if they leak. Bots scan public repositories for keys continuously, so a key pushed to a public repo should be treated as stolen.

For a full pre-launch list, see our vibe coding security checklist.

Working with your AI agent and Git together

A few habits make Git and AI agents work well together:

A daily checklist

If you remember nothing else, remember this:

  1. git status before you start. Clean? Good.
  2. Big or risky change? git switch -c a new branch first.
  3. Prompt the agent for one focused change.
  4. Test the app.
  5. git diff: read what changed, look for surprises and secrets.
  6. Works? git add -A and git commit -m "clear message".
  7. Broken? git restore . and try a different prompt.
  8. End of session: git push.

Where to learn more

The free, official Pro Git book is the best complete reference and is available in many languages. You do not need to read it all; the first three chapters cover everything in this guide in more depth. When you are ready to plan bigger builds, read how to vibe code an app and how to build a SaaS with AI.

FAQ

Do I need Git if my AI tool has its own undo or checkpoints?

Built-in checkpoints are helpful for quick undos inside one session, but they are usually tied to that tool and that session. Git keeps a permanent, tool-independent history, works with any editor or agent, and connects to GitHub for backup and deployment. Use both.

Is GitHub the same as Git?

No. Git is the version-control tool that runs on your computer. GitHub is a website that hosts Git repositories online, with extras like issues, pull requests and deployment integrations. GitLab and Bitbucket are alternatives.

Should my repository be public or private?

Private by default, especially for apps with users or business logic. Make it public only when you intend to open-source it, and double-check that no secrets are in the history first.

What if I already committed my API key?

Rotate the key immediately in the provider's dashboard so the old one stops working. Then remove it from the code, move it to .env, and add .env to .gitignore. Cleaning history is optional once the key is rotated, but rotation is not optional.

About the author

Suraj Kumar builds and edits AIUseFlow, a directory of AI tools for vibe coders, agent builders and creators. How we rank tools.

Get the weekly stack

Rank movers and new AI tools for builders, once a week. Free, no spam, unsubscribe anytime.