Git Workflow
Last reviewed
BeginnerWhat you'll learn
~20 min- Initialize a new git repository with git init
- Use the stage-then-commit workflow (git add, git commit)
- Write clear, descriptive commit messages
- Apply the commit-before-AI pattern to protect your work
By the end of this lesson, you’ll be able to create a git repository, stage your changes, and commit them: the three-step workflow you’ll repeat every time you work on a project. If Git isn’t installed or configured yet (for example, if you jumped here on the Beginner First Session route), ask your agent to install Git and set your name and email first, or see Lesson 7.1.
The Mental Model: Packing a Shipment
Think of saving your work in git like packing a box to ship:
- Your desk is the working directory — files are spread out, some changed, some new.
- The packing table is the staging area — you pick which items go in this particular box.
- Sealing and labeling the box is the commit — you write what’s inside and it’s saved.
You don’t have to pack everything at once. You choose what goes in each box, label it clearly, and stack the sealed boxes on a shelf where you can always open any of them later.
The Three-Step Workflow
Using git day-to-day requires just three steps:
- Make changes to your files (or let the AI do it)
- Stage the changes you want to save (
git add) - Commit them with a message (
git commit)
That’s it. Let’s walk through each one.
Step 0: Create a Repository
Before git can track a project, you need to initialize it in your project folder (your existing ai-class folder from Lesson 1.2, or a new folder such as my-project):
mkdir my-projectcd my-projectgit init(If you are already working in your ai-class folder, skip mkdir and run git init inside ai-class.)
git init creates a hidden .git folder inside your project. This is where git stores all the version history. You never need to touch this folder — git manages it automatically.
You only run git init once per project — when you first create it. After that, git can track changes in this folder, but each new file must be added (git add) before it becomes tracked. If you clone a project from GitHub, it is already initialized.
The hidden .git folder is the repository — it contains your entire version history. If you delete it, all your commit history is gone. Leave it alone and let git manage it.
Make your project a repository
In your ai-class folder, ask your agent:
Turn this folder into a git repository if it is not one already, and tell me what you did.
Then verify it in your own terminal:
git statusIf you see a branch name instead of an error, the folder is a repository.
Step 1: Make Changes
Ask your agent to create starter files (index.html, style.css, and README.md), or create them in your terminal:
# Mac, Linux, or WSL:touch index.htmltouch style.cssecho "# My Project" > README.md# Windows PowerShell:New-Item index.html, style.css -ItemType File -Force"# My Project" | Set-Content README.mdAt this point, git knows these files exist but hasn’t saved them yet. They’re “untracked.”
Step 2: Stage Changes (git add)
Staging tells git which changes you want to include in your next commit. Think of it as putting items on the packing table before sealing the box.
git add index.html style.css README.mdOr, to stage everything:
git add .The . means “everything in the current directory” (except files listed in your .gitignore).
git add . stages everything, including files you might not want to commit — like .env files with API keys or bulky node_modules/ folders. In the exercises here, that’s fine because there are no secrets to leak. In real projects, always set up a .gitignore file first — you’ll learn how in Lesson 4. Until then, prefer staging specific files by name: git add index.html style.css.
Sometimes you’ve made changes to 10 files, but you only want to save 3 of them right now. Staging lets you pick and choose. You can also stage specific files: git add index.html style.css instead of everything. For most learning purposes, git add . (stage everything) works fine as long as your .gitignore is set up.
Step 3: Commit (git commit)
A commit is the actual save point. Every commit has a message describing what changed:
If this is your very first commit ever, git will ask you to identify yourself. Run these two commands once (you did this in Lesson 1, but if you skipped it):
git config --global user.name "Your Name"git config --global user.email "you@example.com"The --global flag means this applies to all git projects on your machine.
git commit -m "Add initial project files"The -m flag lets you write the message inline. The message should be short and describe what you did.
Good commit messages:
"Add login page with form validation""Fix broken link in navigation""Update styles for mobile responsiveness"
Bad commit messages:
"stuff"— too vague"asdfgh"— meaningless"I made some changes"— no useful information
🔍Commit message conventionsclick to expand
Professional developers often follow a pattern: start with a verb in imperative mood (“Add,” “Fix,” “Update,” “Remove”). Think of completing the sentence “This commit will…” So “Add contact form” rather than “Added contact form” or “Adding contact form.” This isn’t a strict rule for learning, but it’s a good habit to build.
The Complete Flow
Here’s the reference pattern you’ll repeat across sessions (you can run these commands yourself in a terminal, or in daily work ask your agent to save a Git checkpoint for you):
# 1. Make changes (or let AI make them)# ... edit files, run AI tools, etc.
# 2. Check what changedgit status
# 3. Stage the changesgit add .
# 4. Commit with a descriptive messagegit commit -m "Add contact form with validation"
# 5. (Optional) Verify your commit was savedgit log --onelineThe git status command (step 2) shows you what’s changed since your last commit. It’s your “what happened?” command — use it frequently. You can also run git status at any time between steps to check where things stand. The git log --oneline at the end shows your commit history in a compact list, confirming your work was saved.
Worked Example: The Commit-Before-AI Pattern
This is the most important git habit you’ll build. Every time you’re about to ask an AI tool to make changes, commit first. Here is a reference session walkthrough showing what happens under the hood (in daily work, your agent can also run these checkpoints for you when you ask):
# You've been working on a project — commit what you havegit add .git commit -m "Working homepage with header and hero section"
# Now ask Claude Code to add a new featureclaude> "Add a contact form to the homepage with name, email, and message fields"
# Claude edits index.html, creates contact.js, modifies style.css# Check what changedgit status# Changes not staged for commit:# modified: index.html# modified: style.css# Untracked files:# contact.js
# It looks good — save this progressgit add .git commit -m "Add contact form with client-side validation"Now suppose you ask Claude for another change and it breaks things:
# Ask Claude for something ambitious> "Redesign the entire page layout to use a sidebar navigation"
# Check the result... the page is broken# No problem — revert to the last working commitgit restore .That one command, git restore ., throws away the unstaged changes in tracked files and, as long as you staged nothing since, restores them to the last commit. (If you already ran git add, use git restore --staged --worktree . instead.) Your contact form is back, working perfectly. This is why you always commit before asking the AI to make big changes.
Your tool’s own rewind, and what it covers
> Redesign the entire page layout to use a sidebar navigation# the page is broken, and you are still in the same Claude Code session/rewind# pick the checkpoint just before that prompt, then restore code and conversationClaude Code checkpoints before each prompt, so /rewind (or Esc twice on an empty prompt) steps back through its own file edits, but changes made by shell commands it ran are not tracked (checkpointing docs). The other tools differ: Antigravity CLI’s /rewind rolls back the conversation and its docs do not say whether files roll back too, so check /diff and git status; Copilot CLI lists /rewind in its help without documenting what it restores; Codex CLI has none, and OpenAI’s own guide says to create git checkpoints before and after a task. Use the tool’s rewind as a shortcut; the commit you made before asking is the undo that works in all four.
If the AI created brand-new files, git restore . will not remove them — they remain as “untracked” files. You will need to delete them manually or use git clean -fd (which permanently removes untracked files and folders). Always check git status after restoring.
🧬In Your Field: Biotechclick to expand
This pattern is especially valuable when you’re using AI to generate or modify analysis scripts. Commit your working pipeline before asking Claude to “optimize the statistical analysis” or “add a new visualization.” If the optimization introduces a bug in your data processing, you can revert instantly instead of debugging someone else’s refactoring of your code.
🏛️In Your Field: Government Devclick to expand
In professional and government environments, traceability matters. Each commit creates a documented record: “At 2:30 PM, the configuration was working. At 2:45 PM, we asked the AI to modify the data processing script. At 2:50 PM, we reverted because the output changed.” This kind of timeline is invaluable when explaining changes to supervisors or during compliance reviews.
The Staging Area Visual
Think of it like preparing a package to ship:
Working Directory → Staging Area → Repository(your files) (git add) (git commit)
index.html ─────────→ index.html ──────→ Commit #1 style.css ─────────→ style.css ──────→ "Add initial files" app.js (not added)Files in the working directory that aren’t staged won’t be included in the commit. This gives you control over exactly what gets saved.
Save your first checkpoint
Ask your agent:
Save a Git checkpoint of what we have so far, and show me the commit log.
You should see one commit with a short message. The agent staged your files and committed them for you.
See what changed
Ask your agent to make a small edit, such as adding a line to your README. Then, in your own terminal, run:
git statusYou should see the file listed as modified. That is what git sees before the next checkpoint.
📊In Your Field: MIS / Businessclick to expand
Think of each commit as a checkpoint in a project management workflow. If you’re building a dashboard with AI assistance and the stakeholder says “actually, go back to what we had Tuesday,” you can find Tuesday’s commit and restore it. In business settings, this kind of rollback capability saves hours of rework and keeps client deliverables on track.
When Things Go Wrong
Use the Symptom → Evidence → Request pattern: describe what you see, paste the error, then ask for a fix.
Key Takeaways
- Three commands are all you need:
git add .to stage,git commit -m "message"to save,git statusto check. - Always commit before asking the AI to make changes. This is your most important safety habit.
- git init starts tracking a folder. You only need to do this once per project.
- Staging gives you control. You choose exactly which changes go into each commit.
- If AI changes break something, revert.
git restore .throws away unstaged changes to tracked files and takes you back to your last save point (add--staged --worktreeif you already staged them).
You're about to ask Claude Code to refactor your entire project. What should you do first?