Git/
Lesson

Imagine AI just rewrote your entire authenticationWhat is authentication?Verifying who a user is, typically through credentials like a password or token. component. It looks good at first glance, but after testing you realize the old version handled edge cases better, and you already saved over it. If only you could go back in time...

That's exactly what Git does. It's a version control system that tracks every change, yours and AI's, creating snapshots you can return to whenever you want. When you're building with AI, Git isn't optional. It's your undo button for every AI-generated change that doesn't work out.

Why Git matters more when AI writes your code

Without Git, accepting an AI suggestion is a one-way door. With Git, every AI-generated change is reversible. You can try AI's approach, compare it against what you had, and roll back in seconds if it's worse.

Never lose work again

When you use Git, every significant version of your code is preserved. Accidentally deleted your database migrationWhat is migration?A versioned script that changes your database structure (add a column, create a table) so every developer and server stays in sync. script? No problem, just rewind to yesterday's commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.. Changed your mind about that refactor? Revert it with one command.

How it works under the hood
Git doesn't just save copies of your files. It stores the differences between versions using a clever compression algorithm called "delta encoding." This means even massive projects with years of history stay lightweight on your disk.

Review AI changes before they stick

When AI generates a large code block, you need to review it line by line before it becomes permanent. Git's diffWhat is diff?A comparison showing exactly which lines were added, removed, or changed between two versions of code. tools let you see exactly what AI changed, compare it against your previous version, and selectively accept or reject parts of the output.

Git also enables branches: separate timelines where you can test AI-generated changes in isolation. If the AI's approach works, merge it. If it doesn't, delete the branch. Your main code stays untouched throughout.

Experiment fearlessly

Want to try rewriting your authenticationWhat is authentication?Verifying who a user is, typically through credentials like a password or token. system? Go ahead, create a branch. If it works, merge it. If it doesn't, delete the branch and pretend it never happened. Your main code stays untouched and stable throughout.

02

The AI trap: committing too often or not enough

AI coding assistants like Copilot and ChatGPT sometimes encourage bad Git habits. They might suggest committing after every single line change, creating a noisy, unreadable history. Or they do the opposite, generate a mountain of code and leave you with one giant commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. labelled "Add features."

AI pitfall
ChatGPT often suggests running git add . && git commit -m "update" after every prompt. This creates a commit history full of meaningless messages. Instead, commit when you reach a logical milestone, a working feature, a fixed bug, a completed refactor. Quality over quantity.
HabitWhat AI suggestsWhat you should do
Commit frequencyAfter every promptAfter each logical milestone
Commit messages"update", "fix", "changes"Descriptive: "Add login form validation"
Staginggit add . every timeStage selectively with git add filename
Review before commitSkip itAlways run git diff --staged first
03

The three areas you need to understand

Git's workflow revolves around three distinct areas. Understanding this flow is crucial, it's the foundation everything else builds upon.

Working directory: where you actually code

This is simply your project folder on your computer. When you open VS Code and edit app.js, you're working in the working directory. Git is watching these files, but it hasn't saved any changes yet.

Staging areaWhat is staging area?A holding zone in Git where you place changes you want to include in your next commit, letting you select exactly which modifications to save.: your pre-flight checklist

Before saving a snapshot (commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed.), you use git add to select which changes you want to include. Think of staging like packing a suitcase, you try on different outfits (edit files), decide what to bring (git add), and only then zip it up (commit).

Pro tip
The staging area is what makes Git so powerful. You can stage just the changes you want, unstage mistakes, or even stage parts of a file while leaving other parts untouched. We'll explore this more in later lessons.

RepositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory.: your project's memory

Once you run git commit, Git permanently stores your staged changes in the repository (specifically, in the hidden .git folder). This snapshot now has a unique ID, timestamp, and your message describing what changed.

Here's how it all flows together:

Working Directory → Staging Area → Repository
   (edit files)      (git add)      (git commit)
AreaWhat's happeningGit command
Working DirectoryYou're actively editing filesNone, just code!
Staging AreaPreparing changes for the next snapshotgit add filename
RepositoryPermanent storage of all saved versionsgit commit -m "message"
04

Git and GitHub: not the same thing

This confusion trips up every beginner, so let's clear it up right now.

Git is a command-line tool that lives on your computer. It tracks changes, creates commits, and manages branches, all locally. You can use Git for years without ever touching the internet.

GitHub is a website (owned by Microsoft) that hosts Git repositories online. It's like Dropbox or Google Drive, but specifically designed for code. GitHub adds features like pull requests, issues, and wikis, but at its core, it's just storing your Git repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory..

You can use Git without GitHub, and you can even use GitHub without fully understanding Git (though I don't recommend it). But together, they're the industry standard for modern software development.

Good to know
There are alternatives to GitHub, GitLab, Bitbucket, Azure DevOps, but they all work with Git. Learn Git once, use it everywhere.
05

Why Git matters more with AI

If you're using AI to generate code, Git is non-negotiable. Here's why it's even more critical in an AI-assisted workflow:

  • Reversibility: AI suggestions are experiments. Git lets you try them and roll back instantly.
  • Diffing: git diff shows you exactly what AI changed, line by line. This is your primary review tool.
  • Selective commits: AI often changes more than you asked for. Git lets you commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. only the parts you want.
  • History: When AI-generated code breaks something three days later, git log and git bisect help you find exactly which change caused it.

Every AI-assisted project should be a Git repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory. from the first prompt.

06

Quick reference

CommandWhat it doesWhen to use it
git --versionCheck if Git is installedFirst time setup
git config --global user.name "You"Set your identityOnce per machine
git config --global user.email "you@email.com"Set your emailOnce per machine
git config --listView current configVerify settings
javascript
# Check if Git is installed
git --version

# Configure your identity (do this once)
git config --global user.name "Your Name"
git config --global user.email "your@email.com"

# Check your configuration
git config --list