Open in VS Code: From your cloned
s26-06643repository, run in the terminal:code sse/02-git-version-control/02-git-version-control.ipynb
Module 02: Git and Version Control#
Track every change with confidence.
Learning Objectives#
Understand why version control is essential
Initialize and manage Git repositories
Create commits, branches, and merges
Use AI to explain Git concepts and resolve errors
Develop good commit habits
Why Version Control?#
Especially important with AI-generated code:
Track changes: See what AI modified
Rollback: Undo bad AI suggestions
Branches: Experiment safely
Collaboration: Work with others (and AI)
History: Understand how code evolved
Rule: Use version control to manage AI changes!
Git Basics#
Configuration#
# Set your identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Check config
git config --list
Initialize a Repository#
# Create new repo
git init
# Clone existing repo
git clone https://github.com/user/repo.git
The Git Workflow#
Working Directory → Staging Area → Repository
(edit) (add) (commit)
Basic Commands#
# Check status
git status
# Stage changes
git add file.py # Specific file
git add . # All changes
git add -p # Interactive staging
# Commit
git commit -m "Add feature X"
# View history
git log
git log --oneline
git log --graph --oneline
AI Help for Git#
> I made changes but want to undo them before committing
Claude: Use git restore:
git restore file.py # Discard changes to specific file
git restore . # Discard all changes
git restore --staged file.py # Unstage a file
Exercise 1: Your First Commits (10 min)#
Let’s practice the basic Git workflow hands-on.
Open your terminal and create a new directory:
mkdir git-practice cd git-practice
Initialize a Git repository:
git initCreate a file called
README.mdwith your name and today’s date:echo "# Git Practice - Your Name - 2026-03-07" > README.md
Run
git status— What does it show? (You should seeREADME.mdlisted as an untracked file in red.)Stage the file:
git add README.md
Run
git statusagain — What changed? (The file should now appear in green under “Changes to be committed.”)Commit:
git commit -m "Add README"
View your commit:
git log
Expected outcome: You should see one commit in the log with your message “Add README”, your name, and a commit hash.
Writing Good Commit Messages#
Format#
type: Short summary (50 chars or less)
Longer description if needed. Wrap at 72 characters.
Explain what and why, not how.
Types#
Type |
Description |
|---|---|
|
New feature |
|
Bug fix |
|
Documentation |
|
Adding tests |
|
Code restructuring |
|
Formatting |
|
Maintenance |
AI-Assisted Commit Messages#
> Write a commit message for these changes: [shows diff]
Claude:
feat: Add user authentication endpoint
- Implement JWT token generation
- Add password hashing with bcrypt
- Create login and logout routes
Exercise 2: Commit Message Practice (5 min)#
You’ve made the following changes to a project. Write a conventional commit message for each one (use the type: description format from above):
(a) Added a new function to calculate the h-index of an author.
(b) Fixed a bug where passing an empty list caused a crash in the analysis pipeline.
(c) Updated the README with installation instructions.
(d) Reformatted all Python files using Black.
Write your answers down, then try this:
Compare with AI: Ask your AI agent — “Write conventional commit messages for each of these scenarios: (a) added h-index function, (b) fixed empty list crash, (c) updated README with install instructions, (d) reformatted code with Black.”
How do your messages compare to what the AI suggested?
Did the AI pick the same
typeprefixes you did?Which messages are more descriptive — yours or the AI’s?
Expected types: (a) feat, (b) fix, (c) docs, (d) style
Branching#
Branches let you develop features in isolation.
# Create and switch to branch
git checkout -b feature-name
# or (modern)
git switch -c feature-name
# List branches
git branch
# Switch branches
git checkout main
git switch main
# Delete branch
git branch -d feature-name
Merging#
# Merge feature into main
git checkout main
git merge feature-name
AI Help with Merge Conflicts#
> I have a merge conflict in file.py. Here's the conflict markers: [shows file]
Claude: The conflict shows two versions:
- Lines between <<<<<<< HEAD and ======= are from main
- Lines between ======= and >>>>>>> feature are from your branch
To resolve, choose which version to keep (or combine them),
remove the conflict markers, then:
git add file.py
git commit
Exercise 3: Branch and Merge (10 min)#
Practice the branching workflow in your git-practice repo:
Create and switch to a new branch:
git checkout -b add-greeting
Create a file called
hello.pywith this content:print("Hello, world!")
Stage and commit it:
git add hello.py git commit -m "feat: Add hello world greeting"
Switch back to main:
git checkout main
Check: Does
hello.pyexist on main? (Runls— it shouldn’t!)Merge the branch:
git merge add-greeting
Check again: Does
hello.pyexist now? (It should!)Delete the branch:
git branch -d add-greeting
View the history:
git log --oneline --graph
Key insight: Branches let you develop in isolation. The file only appeared on main after you merged.
Exercise 4: Create and Resolve a Conflict (10 min)#
Merge conflicts are inevitable — let’s create one on purpose so you know how to handle it.
In your
git-practicerepo, make sure you’re onmain:git checkout main
Edit
README.mdto add a second line:"This is the main branch". Then commit:echo "This is the main branch" >> README.md git add README.md git commit -m "docs: Add branch info to README"
Create and switch to a new branch:
git checkout -b conflict-demo
Edit
README.md— replace the second line with"This is the feature branch". Then commit:# Edit the file (use nano, vim, or your editor) git add README.md git commit -m "docs: Update branch info in README"
Switch back to main and merge:
git checkout main git merge conflict-demo
You should see a merge conflict! Open
README.md— you’ll see conflict markers:<<<<<<< HEAD This is the main branch ======= This is the feature branch >>>>>>> conflict-demo
Resolve it: Edit the file to keep the content you want (or combine both lines). Remove all conflict markers (
<<<<<<<,=======,>>>>>>>).Complete the merge:
git add README.md git commit -m "fix: Resolve merge conflict in README"
If you get stuck: Ask your AI agent — “How do I resolve a merge conflict in git?”
Key takeaway: Conflicts look scary but are just Git asking you to make a decision. The markers show you both versions so you can choose.
Viewing Changes#
# See uncommitted changes
git diff
# See staged changes
git diff --staged
# See changes in a commit
git show abc1234
# See file at specific commit
git show abc1234:path/to/file.py
# Compare branches
git diff main..feature-branch
Understanding Diffs#
- removed line (red)
+ added line (green)
unchanged context
Undoing Things#
Before Committing#
# Discard changes
git restore file.py
# Unstage
git restore --staged file.py
After Committing#
# Amend last commit (change message or add files)
git commit --amend
# Undo last commit but keep changes
git reset --soft HEAD~1
# Undo last commit and discard changes
git reset --hard HEAD~1
# Create a new commit that undoes a previous commit
git revert abc1234
AI Help for Recovery#
> I accidentally committed sensitive data. How do I remove it from history?
Claude: WARNING: This rewrites history. If pushed, coordinate with team.
First install git-filter-repo (a modern replacement for the deprecated git filter-branch):
pip install git-filter-repo
Then remove the file from all history:
git filter-repo --path path/to/file --invert-paths
Then force push: git push --force
.gitignore#
Exclude files from version control.
# Python
__pycache__/
*.py[cod]
*.egg-info/
dist/
build/
.venv/
venv/
# IDE
.vscode/
.idea/
*.swp
# Secrets
.env
*.pem
secrets.json
# OS
.DS_Store
Thumbs.db
AI-Generated .gitignore#
> Create a .gitignore for a Python data science project
Claude: [generates comprehensive .gitignore including
Jupyter checkpoints, data files, model artifacts, etc.]
Exercise 5: Full Version Control Workflow (15 min)#
Put it all together. In your git-practice repo:
Create a new branch called
add-calculatorAsk your AI agent: “Write a simple Python calculator function that handles add, subtract, multiply, and divide.”
Save the AI-generated code to a file called
calculator.pyReview the code before committing — does it handle division by zero?
Stage and commit with a good conventional commit message
Switch to
mainand merge the branchRun
git log --oneline --graphto see the full history
Reflect: At any point, you could have used git diff to see what changed, or git restore to undo something. Version control gives you a safety net — especially valuable when working with AI-generated code.
Summary#
Command |
Purpose |
|---|---|
|
Create repository |
|
Stage changes |
|
Save snapshot |
|
Check state |
|
View history |
|
Manage branches |
|
Combine branches |
|
View changes |
Key habit: Commit early and often, especially before AI makes changes.
Tips and Tricks#
Let AI write your commit messages: Prompt “Write a commit message for these changes: [paste diff]” saves time and produces clearer, more consistent messages.
Commit early, commit often: Small commits are easier to review, easier to revert, and give you more rollback points when experimenting with AI-generated changes.
Commit before letting the agent work: Always commit your current state before asking the agent to make changes. Then use
git diffto see exactly what it did.Use
git stashbefore experimental sessions: If things go wrong during an AI session,git stash popgets you back to a clean state instantly.Ask AI to explain git history: Prompt “Explain what this git log shows: [paste log]” is faster than decoding complex branch histories yourself.
Guard dangerous git operations: Never let the agent run
git push --forceorgit reset --hardwithout understanding why. These can destroy work permanently.Generate
.gitignoreearly: Ask “Generate a .gitignore for a Python project” at the start. This prevents committing virtual environments, cache files, and other junk.Ask AI to diagnose confusing state: If you are confused, prompt “Run git status and git log –oneline -10 and explain what you see.” This is faster than puzzling through it yourself.
Foundational Concepts to Commit to Memory#
Working directory, staging area, repository: These are the three states of your files. You edit in the working directory,
git addmoves changes to staging, andgit commitsaves them permanently. Know where your changes are at all times.Commits are snapshots, not diffs: Each commit records the full state of your project at that moment. This is why you can always go back to any previous state.
Branches are cheap pointers: A branch is just a label pointing to a commit. Creating a branch costs almost nothing. Use them freely to isolate work.
Conventional commit format:
type: descriptionwhere type is one offeat,fix,docs,test,refactor,style,chore. This is not optional style – it is how teams communicate through history.git status is your best friend: Run it constantly. It tells you what branch you are on, what is staged, what is modified, and what is untracked. When in doubt, check status.
Merge conflicts are decisions, not errors: When Git shows conflict markers, it is asking you to choose. Read both versions, decide what the code should be, remove the markers, and commit.
git diff before git add: Always review what you are about to stage. This is especially important with AI-generated changes where you might not have seen every modification.
.gitignore prevents accidents: Set it up at the start of every project. Never commit secrets, build artifacts, or environment-specific files.
Knowing vs. Doing: Reflection#
Git is a tool you can use productively after learning a handful of commands, but it is also a tool you can study for years and still discover new capabilities. That tension is worth sitting with. You do not need to understand the internals of Git’s directed acyclic graph to make commits, create branches, and resolve conflicts. But the more you understand about how Git actually works – that branches are pointers, that commits are snapshots, that the reflog tracks everything – the faster you can recover from mistakes and the more confidently you can work.
AI agents are particularly good at Git. They can generate commit messages, explain diffs, resolve merge conflicts, and construct complex git log queries you would never memorize. This is a genuine superpower: you can ask “how do I undo the last three commits but keep my changes?” and get an accurate answer in seconds instead of searching Stack Overflow for twenty minutes. The AI expands your effective Git knowledge well beyond what you have memorized, and that lets you move faster.
But here is the catch: Git mistakes can be consequential. A bad git reset --hard can destroy work. A force push can overwrite a teammate’s commits. The value you bring is not in knowing every Git command – it is in understanding what operations are safe and what operations are destructive, in knowing when to branch before experimenting, and in building the habit of committing before you let AI change your code. Those instincts only come from practice and from understanding the concepts well enough to know what questions to ask. Let AI handle the syntax. You handle the judgment.
Additional Resources#
Pro Git Book (free) – The definitive Git reference, available free online. Chapters 1-3 cover everything in this lecture and more.
GitHub Git Guide – GitHub’s practical guide to using Git with their platform
Learn Git Branching (interactive) – Visual, interactive tutorial for understanding branches, merges, and rebases
Conventional Commits – The specification for the commit message format used in this course
Next Steps#
In the next module, we’ll learn to work with APIs using Python and AI assistance.