Git basics for AI coders: save points, undo and branches
When an AI agent edits your code, Git is how you get back to a version that worked. This guide covers the few Git commands a beginner needs: save points, checking what changed, undoing safely and trying ideas on a branch. Everything here is quoted from the Pro Git book and Git reference and GitHub's docs, checked Oct 2026.
Not affiliated with or endorsed by the Git project or GitHub. Check their current docs before relying on these details.
The good news, from the Git book: "anything that is committed in Git can almost always be recovered." So commit often.
Step 1: Install Git and tell it who you are
Follow the Git install page for Windows, macOS or Linux. Then check it worked:
git --version
Next, set your name and email. The book says "every Git commit uses this information." You "need to do this only once if you pass the --global option":
git config --global user.name "Your Name"
git config --global user.email you@example.com
Step 2: Start tracking a project
In your project folder, run:
git init
This "creates an empty Git repository", which is a hidden .git folder where Git keeps your history.
Before your first commit, add a .gitignore file. The book calls this "generally a good idea so you don't accidentally commit files that you really don't want in your Git repository." A .gitignore "specifies intentionally untracked files that Git should ignore." GitHub keeps a list of ready-made ones in its github/gitignore repository.
Keep passwords and API keys out of Git. GitHub warns: "Never git add, commit, or push sensitive information, for example passwords or API keys, to a remote repository." See our guide on keeping secrets safe with AI coding tools.
Step 3: Make a save point before every AI change
A commit is a save point. Make one before you ask an agent for a change:
git add -A
git commit -m "Before AI change"
The book explains that "the commit records the snapshot you set up in your staging area," so git add picks what goes in, and git commit saves it.
Step 4: See what the AI changed
git status
git diff
git status is "the main tool you use to determine which files are in which state." git diff "shows you the exact lines added and removed." To see your past save points, run git log, which shows "the most recent commits" first.
For a full checklist of what to look for, see our guide on how to review AI-written code before you keep it.
Step 5: Undo safely
The book warns that undoing "is one of the few areas in Git where you may lose some work if you do it wrong." So go slow here.
Take a file out of the next commit without losing your edits:
git restore --staged path/to/file
Throw away changes to one file. The book says: "git restore <file> is a dangerous command. Any local changes you made to that file are gone — Git just replaced that file with the last staged or committed version."
git restore path/to/file
Undo a commit you already made, the safe way. git revert will "record some new commits to reverse the effect of some earlier commits," so your history stays intact:
git revert <commit>
You can copy a commit's ID from git log.
Commands to be careful with
These can delete work for good. Read the warnings before you run them, and don't let an agent run them without asking you.
git reset --hard: the book says this flag is "one of the very few cases where Git will actually destroy data." It will "Overwrite all files and directories with the version from <commit>, and may overwrite untracked files."git clean: "You'll want to be pretty careful with this command, since it's designed to remove files from your working directory that are not tracked."git push --force: the docs say it "can cause the remote repository to lose commits; use it with care."- Amending: "Only amend commits that are still local and have not been pushed somewhere."
Step 6: Try ideas on a branch
A branch lets an agent try something without touching your working version. Create one and switch to it:
git switch -c try-ai-idea
The -c option will "Create a new branch named <new-branch> ... before switching to the branch." Switching stops if it "leads to loss of local changes," so commit or stash your work before you switch branches.
Stash puts unfinished work aside. Use it "when you want to record the current state of the working directory and the index, but want to go back to a clean working directory." To bring it back, the book says "You can also run git stash pop to apply the stash and then immediately drop it from your stack."
If the idea worked, switch back and merge it. If you "changed the same part of the same file differently in the two branches," Git will report a conflict, and you'll need to fix those files and commit.
If the idea didn't work, just switch back to main and your working version is untouched.
Step 7: Back it up on GitHub
Create a new repository on GitHub, then push your commits. GitHub says "you usually run git push origin main to push your local changes to your online repository." The first time, GitHub's docs show:
git push -u origin main
If you already committed a file you meant to ignore, GitHub says "you must untrack the file before you add a rule to ignore it," using git rm --cached FILENAME.
Git only saves what you commit. The Pro Git book says Git "won't start including" an untracked file "in your commit snapshots until you explicitly tell it to do so." Our advice, not a vendor quote: that means your .env file (which you should ignore), untracked files and your app's database aren't in Git, so back them up another way.
Next steps
- Review what the AI changed: how to review AI-written code
- Set up your tool: Claude Code, Codex CLI, Cursor or Copilot in VS Code
Not affiliated with or endorsed by the Git project or GitHub. Check their current docs before relying on these details.