Eight git commands that save your project.

Commit, branch, diff, restore, stash, log, revert and bisect, each run in a real repository with the output it printed.

Railway tracks splitting and joining at a switch
Photo by Mihai Lazăr on Unsplashdithered by Cyborb

Git is the undo button for AI coding. Commit your project before every agent task, give the task its own branch, and read the diff when the agent stops. If the result is wrong, one command puts your files back exactly as they were.

This is git for beginners who code with AI: the one-time setup, then the eight commands that do that work. We ran each one in a scratch repository with Git 2.50, and the output shown is what Git printed.

The short version
  • Git saves snapshots of your project called commits. Anything you committed can come back, even after a bad agent run.
  • Commit before every agent task, and give each task its own branch.
  • When the agent stops, run git status and git diff. New files only show up in git status.
  • Undo with git restore for a file, git stash for a whole attempt and git revert for a commit.
  • If something broke and you do not know when, git bisect finds the commit in a handful of tests.

Why git matters more when an AI writes the code

A coding agent can change ten files in a minute, and nobody remembers what they looked like before. Git does, but only for work you committed. So the habit matters more than the commands: with a commit before each task, the most you can lose is one attempt. It is the first guardrail to add if you vibe code.

The eight commands, in the order you will reach for them:

#CommandWhat it doesWhen you need it
1git commitSaves a snapshotBefore every agent task
2git switch -cCreates a branch and moves onto itTo keep each task separate
3git diffShows every changed lineWhen the agent stops
4git restorePuts a file back to its last commitWhen one edit is wrong
5git stashSets all current changes asideWhen a whole attempt is wrong
6git logLists commits, newest firstTo find a commit’s hash
7git revertAdds a commit that undoes an old oneWhen a bad commit already landed
8git bisectSearches history for the commit that broke somethingWhen you do not know what broke it

Git for beginners: set it up once

Open a terminal and run git --version. If it prints a version number, git is installed. If not, get it from git-scm.com.

Then tell git who you are. Every commit records this name and email, so use the email you plan to use on GitHub:

Terminal
git config --global user.name "Ada Lovelace"
git config --global user.email "ada@example.com"
git config --global init.defaultBranch main

The third line names your first branch main instead of the older default, master. Our example project is a six-line Python file that prints a checkout total, 22.5:

shop.py
def total(prices, discount=0):
    subtotal = sum(prices)
    return round(subtotal * (1 - discount), 2)

if __name__ == "__main__":
    print(total([19.99, 5.01], discount=0.1))

Before the first commit, create a file named .gitignore that lists what git must never save. Put .env in it on day one, because a key that reaches GitHub is a key you have to replace. JavaScript projects add node_modules/ too.

.gitignore
.env
__pycache__/

Now create the repository and make your first commit:

Terminal
git init
git add -A
git commit -m "Checkout total works"
Output
[main (root-commit) b9d0b6f] Checkout total works
 2 files changed, 8 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 shop.py

git add -A stages every change in the project, and git commit saves what you staged. b9d0b6f is the start of this commit’s hash. Yours will be different.

Before each agent task: commit and branch

1. git commit: save a checkpoint

Before you hand the agent a task, run git status. If it says nothing to commit, working tree clean, you have a checkpoint. If it lists changed files, commit them first with git add -A and git commit -m "Checkpoint". Whatever the agent does next, you can get back to this exact state.

2. git switch: give the task its own branch

A branch is a separate line of commits. The agent works on the branch, and main stays untouched until you decide to keep the result.

Terminal
git switch -c agent/coupons

Git answers Switched to a new branch 'agent/coupons'. The agent/ prefix is just a habit that makes these branches easy to spot. Older tutorials use git checkout -b instead.

It also helps to tell the agent your git rules up front, once per project:

PromptGit rules for a coding agent
Follow these git rules in this project:
1. Stay on the current branch. Do not switch branches, commit, merge or push unless I ask.
2. Never run commands that throw away work or rewrite history, such as git reset --hard, git clean or git push --force.
3. If you think one of those is needed, stop and tell me why.
4. When you finish, list every file you created, changed or deleted.

After the agent stops: read the diff

3. git diff: see every changed line

Start with the list of files, then read the changes themselves:

Terminal
git status --short
git diff
Output of git status --short
 M shop.py
?? coupons.py

M means modified. ?? means a new file that git does not track yet. Here is the diff, where lines starting with - were removed and lines starting with + were added:

Diff
diff --git a/shop.py b/shop.py
index 9aab286..48ca3f5 100644
--- a/shop.py
+++ b/shop.py
@@ -1,6 +1,11 @@
-def total(prices, discount=0):
+from coupons import COUPONS
+
+
+def total(prices, discount=0, coupon=None):
     subtotal = sum(prices)
-    return round(subtotal * (1 - discount), 2)
+    if coupon:
+        discount = COUPONS.get(coupon, 0)
+    return int(subtotal * (1 - discount))
 
 if __name__ == "__main__":
-    print(total([19.99, 5.01], discount=0.1))
+    print(total([19.99, 5.01], coupon="WELCOME10"))

The coupon logic is what we asked for. But one line is not: the agent swapped round(..., 2) for int(...), which throws away the cents. The script now prints 22 instead of 22.5. For bigger diffs, use our method to review AI-written code by risk.

How to undo AI changes with git

4. git restore: put one file back

The rounding change is wrong, so throw away that file’s edits:

Terminal
git restore shop.py

shop.py is back to its last commit, and the script prints 22.5 again. But git status --short still shows ?? coupons.py, because restore leaves new files alone. git restore . resets every tracked file at once.

One trap: if you already staged the change with git add, plain git restore does nothing. Use git restore --staged --worktree shop.py to reset both copies.

5. git stash: set a whole attempt aside

When the whole attempt is wrong but you might want parts of it later, stash it. The -u flag includes new files, which a plain git stash leaves behind.

Terminal
git stash push -u -m "coupons, attempt 1"
git stash list
Output of git stash list
stash@{0}: On agent/coupons: coupons, attempt 1

Your folder is back at the last commit, and .env stays put because git ignores it. Bring the attempt back with git stash pop, or delete it with git stash drop.

When an attempt is right, keep it: commit on the branch, then merge it into main.

Terminal
git add -A
git commit -m "Add WELCOME10 coupon"
git switch main
git merge agent/coupons

Fix problems you find later: log, revert and bisect

6. git log: find the commit

Terminal
git log --oneline
Output
b05149b Add shipping rule
7813c0a Add VIP20 coupon
800b1a9 Add WELCOME10 coupon
b9d0b6f Checkout total works

Each line is one commit, newest first. The code at the start is the short hash that other commands take. Add --stat to see which files each commit touched.

7. git revert: undo a commit safely

Say the VIP20 coupon should never have shipped. git revert adds a new commit that does the exact opposite of the old one:

Terminal
git revert --no-edit 7813c0a

Git replies with the new commit, [main f91b873] Revert "Add VIP20 coupon". History keeps both commits, which is why revert is the safe choice after you push. Commands that delete commits instead, such as git reset --hard, cause trouble for anyone who already has them.

8. git bisect: find the commit that broke it

A week later the total prints 22 again, seven commits after a version you know worked. Instead of reading them all, let git binary-search them. Name one bad commit and one good one:

Terminal
git bisect start
git bisect bad            # the current version is broken
git bisect good 800b1a9   # this older version worked

Git checks out a commit halfway between. Test it (here, python3 shop.py), then type git bisect good or git bisect bad. After three tests, git named the culprit:

Output
bb9a6af05d09c827705143e3154bb35c66d6242e is the first bad commit
commit bb9a6af05d09c827705143e3154bb35c66d6242e
Author: Ada Lovelace <ada@example.com>
Date:   Wed Sep 23 00:29:56 2026 -0700

    Tidy up total()

 shop.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Run git bisect reset to return to where you started, then git revert --no-edit bb9a6af. The total is 22.5 again. Bisect can also run a test for you at each step, which our guide to debugging with AI shows.

Push your project to GitHub

Commits live only on your computer until you push them. A copy on GitHub survives a lost laptop, and it is where pull requests happen.

GitHub no longer accepts your account password for git commands. The simplest way to sign in is the GitHub CLI: run gh auth login, choose HTTPS, and answer yes when it offers to authenticate git with your GitHub credentials. Then create the repository and push in one step:

Terminal
gh repo create shop --private --source=. --push

Or, if you already created an empty repository on github.com:

Terminal
git remote add origin https://github.com/YOUR-NAME/shop.git
git push -u origin main

The -u flag links your local main to the copy on GitHub, so from then on git push alone is enough. We ran the push against a local test remote. Signing in and creating the repository need your own GitHub account.

Every agent task0 of 6

FAQ

Is git the same as GitHub?

No. Git is the program on your computer that records history. GitHub is a website that hosts copies of git repositories and adds tools such as pull requests. Git works fine without it.

I ran git reset --hard. Is my work gone?

Committed work can usually come back. git reflog lists the commits you were recently on, including ones a reset skipped past. Run git reset --hard with a hash from that list to return. Uncommitted changes are gone, because git never saved them.

Should I let the AI agent run git commands?

Yes for commands that only read, such as git status, git diff and git log. They help the agent understand your project. Anything that deletes work or rewrites history deserves your approval every time.

What is the difference between git revert and git reset?

git revert adds a new commit that undoes an old one, so it is safe after pushing. git reset moves your branch back and can discard commits and uncommitted work. Use reset only on work nobody else has.

Read next: the AI pair programming workflow that ships, or how tests keep a coding agent honest.

Sources
  1. git-restore documentation, Git, version 2.55.0
  2. About authentication to GitHub, GitHub Docs, accessed September 2026
  3. Caching your GitHub credentials in Git, GitHub Docs, accessed September 2026
cyborb.ai

Stop reading about it. Build it.

Describe what you want in plain words. Cyborb plans the work, writes and runs the code, makes the assets, and puts the result online.

Download Cyborb

Free to start. No card required.