Building Module 7 · Git: Your Safety Net

Git Workflow

What 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.

The Mental Model: Packing a Shipment

Think of saving your work in git like packing a box to ship:

  1. Your desk is the working directory — files are spread out, some changed, some new.
  2. The packing table is the staging area — you pick which items go in this particular box.
  3. 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:

  1. Make changes to your files (or let the AI do it)
  2. Stage the changes you want to save (git add)
  3. 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:

Terminal window
mkdir my-project
cd my-project
git init

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.

Only init once

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.

Never delete the .git folder

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.

TRY ITInitialize a new git repository in the current directory
$

Step 1: Make Changes

Create some files (or let your AI tool create them):

Terminal window
touch index.html
touch style.css
echo "# My Project" > README.md

At 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.

Terminal window
git add index.html style.css README.md

Or, to stage everything:

Terminal window
git add .

The . means “everything in the current directory” (except files listed in your .gitignore).

Watch out for secrets

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.

💡Why is staging a separate step?

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:

First-time setup required

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):

Terminal window
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.

Terminal window
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 conventions

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 pattern you’ll repeat hundreds of times:

Terminal window
# 1. Make changes (or let AI make them)
# ... edit files, run AI tools, etc.
# 2. Check what changed
git status
# 3. Stage the changes
git add .
# 4. Commit with a descriptive message
git commit -m "Add contact form with validation"
# 5. (Optional) Verify your commit was saved
git log --oneline

The 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’s a complete walkthrough:

Terminal window
# You've been working on a project — commit what you have
git add .
git commit -m "Working homepage with header and hero section"
# Now ask Claude Code to add a new feature
claude
> "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 changed
git status
# modified: index.html
# modified: style.css
# new file: contact.js
# It looks good — save this progress
git add .
git commit -m "Add contact form with client-side validation"

Now suppose you ask Claude for another change and it breaks things:

Terminal window
# 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 commit
git restore .

That one command — git restore . — throws away all uncommitted changes in tracked files and restores them to the last commit. Your contact form is back, working perfectly. This is why you always commit before asking the AI to make big changes.

New files are not removed by git restore

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 / State 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.

TRY ITStage all files and create a commit with the message 'Initial commit'
$
TRY ITCheck which files have changed since your last commit
$
📊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.

Symptom
git says 'not a git repository' when I try to commit
Evidence
fatal: not a git repository (or any of the parent directories): .git
What to ask the AI
"I'm getting a 'not a git repository' error. I think I forgot to run git init, or I'm in the wrong directory. How do I check where I am and initialize git?"
Symptom
Commit fails saying 'nothing to commit'
Evidence
nothing to commit, working tree clean
What to ask the AI
"Git says there's nothing to commit. I thought I made changes. How do I check if my files are staged, and how do I stage them?"
Symptom
Commit fails asking for identity
Evidence
Author identity unknown. Please tell me who you are.
What to ask the AI
"Git is asking me to set my name and email before I can commit. What are the git config commands I need to run?"
Symptom
I staged the wrong file and want to un-stage it
Evidence
Accidentally ran git add on a file I don't want to commit
What to ask the AI
"I accidentally staged a file I don't want to commit. How do I un-stage it without losing my changes?"

Key Takeaways

  • Three commands are all you need: git add . to stage, git commit -m "message" to save, git status to 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 all uncommitted changes to tracked files and takes you back to your last save point.
KNOWLEDGE CHECK

You're about to ask Claude Code to refactor your entire project. What should you do first?