Index Ai Academy
Phase 0 · Tools · Developer tools

Version control with git

Save snapshots of your work, undo mistakes, and share code.

About 30 minutes · Free lesson

Start this lesson

Why it matters

Every AI project you'll download, from a small prompt library to a model's training code, lives in a git repository, usually on GitHub. Git is also your safety net. Before you let an AI assistant edit your code, make a commit. Then you can always see exactly what it changed, and get back to the version that worked.

What you'll be able to do

  • Turn a folder into a git repository and save snapshots of it with commits
  • Explain the working directory, the staging area and the commit history
  • Read git status, git log and git diff to see what changed
  • Try an idea safely on a branch

What it covers

  1. Snapshots instead of finalv3REAL.docx
  2. Three places a change can be
  3. Good commits
  4. Branches: a safe place to experiment
  5. Remotes and GitHub
  6. Worked example: undo an AI assistant's edit

Snapshots instead of final_v3_REAL.docx

You've probably seen a folder like this: landing-page-v2.html, landing-page-v2-edits.html, landing-page-FINAL.html. Version control fixes that mess. Git records snapshots of a whole folder over time. Each snapshot is called a commit, and it stores exactly what every file looked like at that moment, plus a short message saying why it changed.

A folder whose history git is tracking is called a repository (or "repo"). With one you can:

  • see every change ever made, when, and why;
  • get back any earlier version of any file;
  • try risky ideas without breaking the version that works;
  • share the project and combine work from several people.

Git is the program on your computer. GitHub is a website that stores copies of git repositories online and adds collaboration features on top. You can use git without GitHub, but most teams use both.

Three places a change can be

Git asks you to choose what goes into each snapshot. So a change moves through three places:

PlaceWhat it isHow things get there
Working directoryyour files as you're editing them right nowyou edit a file
Staging areathe changes you've picked for the next commitgit add
Historythe saved commitsgit commit

Think of packing a box to ship. The working directory is your desk, with everything spread out. git add puts chosen items in the box. git commit seals the box and writes a label on it. Things left on the desk aren't in the box, even if you changed them.

The command you'll run most is git status. It tells you which files changed, which changes are staged, and which files git isn't tracking yet. When in doubt, run it.

git init                        # start tracking this folder (once per project)
git status                      # what's changed? what's staged?
git add index.html              # stage one file
git add .                       # or stage every change in this folder
git commit -m "Add landing page"   # save the staged changes as a commit
git log --oneline               # list commits, newest first

Try it below. The landing-page folder isn't a repository yet.

Quick check
What does this print?
staged = ["app.py"]
changed = ["app.py", "notes.txt"]
commit = [f for f in changed if f in staged]
print(commit)
  1. ['app.py', 'notes.txt']
  2. []
  3. ['notes.txt']
  4. ['app.py']
Show the answer

['app.py']

Only app.py is in both lists, so the comprehension keeps just that: ['app.py']. A commit only takes what you've staged. notes.txt changed, but it stays out until you add it.

Try it now

Notice that after the commit, git status still lists styles.css and README.md: you only staged index.html.

Quick check
You edit styles.css and run git commit -m "Update colors" without running git add first. What happens to your edit?
  1. It isn't committed. It stays as an unstaged change in your working directory.
  2. Git commits it but warns you afterwards
  3. It's deleted, because unstaged changes are discarded on commit
  4. It's committed, because commit saves every changed file
Show the answer

It isn't committed. It stays as an unstaged change in your working directory.

Only staged changes go into a commit. With nothing staged, git says there's nothing to commit, and your edit stays on your desk until you git add it.

Good commits

Each commit gets a unique ID (a hash), like 3f9a2c1, plus your message, your name and the time. git log --oneline shows one line per commit, newest first:

a41c9e2 Add pricing section
3f9a2c1 Shorten hero headline
0b7d115 Add landing page

Write messages that say what the commit does, as if finishing the sentence "This commit will...": Add pricing section, Fix broken signup link. "Changes" or "stuff" tells your future self nothing.

Commit small and often: one change per commit, whenever something works. Small commits are easy to understand and easy to undo.

Two more commands help you see what's going on before you commit:

  • git diff shows the exact lines you've changed but not yet staged, with removed lines marked - and added lines marked +.
  • git restore index.html throws away your uncommitted edits to that file. It puts back the last committed version. It's an undo button for work you haven't committed, and it's permanent: those edits are gone.

Branches: a safe place to experiment

A branch is a separate line of commits. Think of a copy of a document you scribble on, while the clean copy stays in the drawer. Every repository starts with one branch, usually called main. When you want to try something, such as a new headline or a risky redesign, you create a branch and commit there. main stays exactly as it was.

git switch -c new-headline    # create a branch and move onto it
git branch                    # list branches; * marks the one you're on
git switch main               # go back to main

(git checkout -b new-headline is the older way to write the first command. You'll see both.)

If the experiment works, the branch is merged back into main, combining its commits. On GitHub this usually happens through a pull request. That's a page showing the branch's changes, so someone can review them before they're merged. If the experiment fails, you switch back to main and delete the branch. Nothing was ever at risk.

Quick check
You want to try a new headline without risking the version that works. What do you do first?
  1. Create a branch and work there
  2. Copy the folder by hand
  3. Commit straight to main
  4. Delete the old file
Show the answer

Create a branch and work there

A branch is a safe place to experiment. If it works you merge it, and if it doesn't you throw it away.

Remotes and GitHub

A remote is a copy of the repository somewhere else, typically on GitHub. By convention the main remote is called origin.

  • git clone <url> downloads a repository, with its whole history, onto your computer.
  • git push sends your new commits up to the remote.
  • git pull brings down commits other people pushed.

The simulator here has no remote, so git push won't work in it. On your own machine, you'd create a repository on GitHub, connect it once, and push.

Some files should never be committed: passwords and API keys, a .env file holding them, large data files, and installed packages. You list them in a file called .gitignore, and git ignores them. A public GitHub repository is visible to everyone, including bots that scan for leaked keys. The API keys lesson comes back to this.

Quick check
What does git add do?
  1. Uploads your changes to GitHub
  2. Marks changes to include in the next commit
  3. Saves a snapshot to the history
  4. Creates a new file
Show the answer

Marks changes to include in the next commit

git add stages changes. git commit saves the snapshot, and git push uploads commits to a remote.

Worked example: undo an AI assistant's edit

You ask an AI assistant to shorten the headline in index.html, and it also rewrites half the page. Git lets you see what happened and choose what to keep. Lines starting with % are what you type. The diff is trimmed to just the changed lines:

% git status
On branch main
Changes not staged for commit:
        modified:   index.html
% git diff index.html
-<h1>Rain shells built for the commute, not the catwalk</h1>
+<h1>Rain shells for the commute</h1>
-<p>Free returns for 60 days.</p>
+<p>Unbeatable deals on every jacket!</p>
% git restore index.html
% git status
On branch main
nothing to commit, working tree clean

Step by step:

  1. git status shows one changed file.
  2. git diff shows the headline change you wanted, plus a change you didn't: the returns promise was replaced with hype.
  3. git restore puts the file back to the last commit, and git status confirms it's clean.

Then you ask again, more precisely: "change only the h1". Because you committed before the assistant started, nothing you'd saved was ever at risk.

Quick check
Which file should go in .gitignore rather than into a commit?
  1. styles.css, the page's colors
  2. .env, which holds your API key
  3. README.md, the project's instructions
  4. index.html, the landing page
Show the answer

.env, which holds your API key

Secrets never belong in git history, where anyone with a copy of the repository can read them. The other files are the project itself.

Practice what you just read

  • a practice exercise that checks your code
  • a 5-question quiz
  • 5 review cards for spaced repetition

Start this lesson

Try a quiz question

What's the relationship between git and GitHub?

  1. Git tracks versions on your computer; GitHub hosts copies of repositories online for sharing and collaboration
  2. They're two names for the same program: GitHub is the newer name, and git is the command you type
  3. GitHub is required for git to work, because every commit is stored on its servers and not on your computer
  4. Git is GitHub's paid desktop app, which adds history and branches on top of the free repositories on the site

Answer it at the end of the lesson to see if you're right, and why.

Before this lesson