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." (checked Oct 2026) Our advice, not a vendor quote: 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" (checked Oct 2026). You "need to do this only once if you pass the --global option" (checked Oct 2026):
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" (checked Oct 2026), 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." (checked Oct 2026) A .gitignore "specifies intentionally untracked files that Git should ignore." (checked Oct 2026) 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." (checked Oct 2026) 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" (checked Oct 2026), 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" (checked Oct 2026). git diff "shows you the exact lines added and removed" (checked Oct 2026). To see your past save points, run git log, which shows "the most recent commits" (checked Oct 2026) 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." (checked Oct 2026) Our advice, not a vendor quote: 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 Git book warns that discarding a file's changes this way means "Any local changes you made to that file are gone" (checked Oct 2026). Use git restore with care:
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" (checked Oct 2026), 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. Our advice, not a vendor quote: 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." (checked Oct 2026) The git reset docs say it will "Overwrite all files and directories with the version from <commit>, and may overwrite untracked files." (checked Oct 2026)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." (checked Oct 2026)git push --force: the docs say it "can cause the remote repository to lose commits; use it with care." (checked Oct 2026)- Amending: "Only amend commits that are still local and have not been pushed somewhere." (checked Oct 2026)
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> starting at <start-point> before switching to the branch." (checked Oct 2026) Switching stops if it "leads to loss of local changes" (checked Oct 2026). Our advice, not a vendor quote: 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." (checked Oct 2026) 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." (checked Oct 2026)
If the idea worked, switch back and merge it. If you "changed the same part of the same file differently in the two branches" (checked Oct 2026), Git will report a conflict, and you'll need to fix those files and commit.
Our advice, not a vendor quote: 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." (checked Oct 2026) 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" (checked Oct 2026), using git rm --cached FILENAME.
Git only saves what you commit. The Pro Git book says Git "won't start including" (checked Oct 2026) an untracked file "in your commit snapshots until you explicitly tell it to do so." (checked Oct 2026) 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.