Learn Go with AI · Free online book

Appendix B: Just enough Git

In short: Appendix B covers only the Git you need for this book: setting it up, committing each step with a Learned: line, checking what the AI changed before and after it edits your code, and undoing changes you don't want.

Git keeps the history of your project. This book uses it for three jobs: saving each step with a Learned: line, seeing exactly what the AI changed, and undoing changes you don’t want. This appendix covers only the commands the book uses.

Last checked: November 2026, with Git 2.43 and 2.47 on Linux. Steps marked (Not tested for this edition) come from official documentation but weren’t run for this edition. In the listings, <hash> stands for a short code like 2522f77; yours will differ.

B.1 Install and set up Git (once)

Check whether you already have Git:

git --version

If you don’t, install it (Not tested for this edition):

Then tell Git who you are. Every commit records it:

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

Without this, your first git commit fails with “Author identity unknown.”

B.2 Start a project

In your project folder, run:

git init

Git may print several hint: lines about the name of the first branch. They’re safe to ignore. If you’d like them gone for future projects, run git config --global init.defaultBranch main once.

Keep files out of Git: .gitignore

Some files don’t belong in Git, such as personal data, secrets and files your tools create. List them in a file named .gitignore in your project folder, one per line. Tracker’s has one line:

expenses.json

Name the file exactly .gitignore, with no .txt at the end. (Notepad on Windows likes to add one.)

.gitignore only works for files Git isn’t tracking yet. If you already committed a file, tell Git to stop tracking it, without deleting it from your disk:

git rm --cached expenses.json

Git prints rm 'expenses.json', and git status --short now shows D expenses.json: deleted from Git, not from your disk. Commit, and Git stops tracking it.

Anything you commit stays in your project’s history even after you delete it. That’s why personal data and secrets should never go into Git in the first place.

B.3 The everyday loop: status, add, commit

See what changed:

git status --short
 M main.go
?? notes.txt

M means a file Git knows was modified. ?? means a new file Git hasn’t seen before. No output means nothing changed since your last commit.

A letter in the first column means the change is staged (after git add): A for a new file, M for modified, D for deleted. A letter in the second column means it isn’t staged yet.

Save a step: stage everything, then commit with a title and a Learned: line:

git add .
git commit -m "Print a welcome message" -m "Learned: Go starts at func main in package main, and won't compile with unused imports."

Git answers with a line like [master (root-commit) <hash>] Print a welcome message and a summary of how many files and lines changed. Later commits don’t say root-commit. If you set init.defaultBranch main before git init, you’ll see main instead of master. Both are fine.

git add . stages every change in the folder, except the files in .gitignore. Each -m adds one paragraph to the commit message: the first is the title, and the second is your Learned: line.

B.4 Writing a good Learned: line

The Learned: line is the Explain step of the SURE loop, written down. Over the book, these lines become your learning journal. A good one is:

Weak Better
Learned: maps Learned: a missing map key gives the zero value, so use the comma-ok form to tell missing from zero.
Learned: fixed a bug Learned: a method needs a pointer receiver to change its value, or it only changes a copy.
Learned: errors are important Learned: %w keeps the original error inside, so errors.Is can still find it.

If you can’t write a specific Learned: line, that’s a sign: go back and ask about the part you’re unsure of.

B.5 Before and after the AI changes code

Before: commit everything first. Then everything the AI changes is easy to see, and easy to undo, because Git compares the files with your last commit.

After: check what the AI touched.

git status --short

If a file appears that you didn’t ask the AI to change, especially a test file, that’s the first thing to look at.

To read the changes line by line:

git diff

Lines starting with + were added, lines with - were removed, and lines starting with a space are unchanged, shown for context. Each block starts with an @@ line saying where in the file it is. Chapter 6 walks through a full example.

git diff doesn’t show new files. To include them, stage everything first, then look at what’s staged:

git add .
git diff --staged --stat
git diff --staged

--stat gives a summary first: which files changed and how many lines.

B.6 Undo changes

Undo the changes to one file and go back to your last commit:

git restore main.go

Undo changes to all files Git already knows:

git restore .

If you’ve already run git add ., unstage first, then restore:

git restore --staged .
git restore .

Remove new files and folders (the ?? ones), such as files the AI created. First see what would go:

git clean -nd
Would remove helper.go
Would remove util/

-n only lists, and -d includes new folders. If that list is right, remove them:

git clean -fd

Be careful: this deletes every new file Git doesn’t know yet, including your own if you haven’t committed them, and they can’t be brought back. That’s why you commit before asking the AI, and why you always run git clean -nd first. Files listed in .gitignore, like expenses.json, are left alone.

B.7 Read your learning journal

One line per commit:

git log --oneline
<hash> Add tests, format an expense and total amounts
<hash> Print a welcome message

Just your Learned: lines, with their titles:

git log --grep="Learned:" --format="%h %s%n    %b"
<hash> Add tests, format an expense and total amounts
    Learned: store money as whole cents in an int, because a float64 can't hold 0.1 exactly.

<hash> Print a welcome message
    Learned: Go starts at func main in package main, and won't compile with unused imports.

--grep is case-sensitive, so write Learned: with a capital L every time. Commits without one, like Chapter 5’s Add Count test, don’t appear.

Newest first. At the end of the book, run it and read your journal from the bottom up: it’s the story of how you learned Go.