Git Lesson Plan
A progressive curriculum to master git through hands-on practice.
Lesson 1: First Commit
Section titled “Lesson 1: First Commit”Goal: Understand the basic commit workflow.
Concepts
Section titled “Concepts”Git tracks changes to files. The workflow is:
- Make changes to files
- Stage changes (select what to commit)
- Commit (save a snapshot with a message)
Exercises
Section titled “Exercises”-
Initialize a repository
Terminal window mkdir learn-git && cd learn-gitgit initgit status # See the empty repo -
Create and stage a file
Terminal window echo "# My Project" > README.mdgit status # Untracked filegit add README.mdgit status # Staged for commit -
Make your first commit
Terminal window git commit -m "Add README"git log # See your commit -
Modify and commit again
Terminal window echo "This is a demo." >> README.mdgit diff # See changesgit add README.mdgit commit -m "Add description to README"git log --oneline # Compact history
Checkpoint
Section titled “Checkpoint”You can init, add, commit, and view history. Run git log --oneline and see 2
commits.
Lesson 2: Branching
Section titled “Lesson 2: Branching”Goal: Work on features without affecting main.
Concepts
Section titled “Concepts”Branches are independent lines of development. main is the default branch.
Create branches for features, merge when done.
main: A---B---C \feature: D---EExercises
Section titled “Exercises”-
Create and switch branches
Terminal window git branch # List branchesgit branch feature # Create branchgit switch feature # Switch to it# Or combined:git switch -c feature2 # Create and switch -
Make commits on a branch
Terminal window git switch -c add-licenseecho "MIT License" > LICENSEgit add LICENSEgit commit -m "Add MIT license"git log --oneline # See commit on branch -
Compare branches
Terminal window git switch mainls # No LICENSE filegit log --oneline --all --graph # Visualize branches -
Delete a branch
Terminal window git branch -d add-license # Fails: not mergedgit branch -D add-license # Force delete
Checkpoint
Section titled “Checkpoint”Create a branch, commit to it, switch back to main. The commit only exists on your branch.
Lesson 3: Merging
Section titled “Lesson 3: Merging”Goal: Combine branch work back into main.
Concepts
Section titled “Concepts”Merge brings changes from one branch into another. Fast-forward merges move the pointer. Three-way merges create a merge commit.
Exercises
Section titled “Exercises”-
Fast-forward merge
Terminal window git switch -c docsecho "## Usage" >> README.mdgit add README.mdgit commit -m "Add usage section"git switch maingit merge docs # Fast-forwardgit log --oneline # Linear history -
Three-way merge
Terminal window # Create diverging branchesgit switch -c feature-aecho "Feature A" > a.txtgit add a.txt && git commit -m "Add feature A"git switch maingit switch -c feature-becho "Feature B" > b.txtgit add b.txt && git commit -m "Add feature B"git switch maingit merge feature-a # Fast-forwardgit merge feature-b # Creates merge commitgit log --oneline --graph -
Handle a merge conflict
Terminal window git switch -c conflict-demoecho "Line from branch" >> README.mdgit add README.md && git commit -m "Branch change"git switch mainecho "Line from main" >> README.mdgit add README.md && git commit -m "Main change"git merge conflict-demo# CONFLICT! Edit README.md to resolve# Remove <<<<<<<, =======, >>>>>>> markersgit add README.mdgit commit -m "Resolve merge conflict"
Checkpoint
Section titled “Checkpoint”Merge two branches. Resolve at least one conflict manually.
Lesson 4: Remote Repositories
Section titled “Lesson 4: Remote Repositories”Goal: Push and pull from GitHub/GitLab.
Concepts
Section titled “Concepts”Remotes are copies of your repo on a server. origin is the conventional name
for your primary remote. Push sends commits; pull fetches and merges.
Exercises
Section titled “Exercises”-
Clone an existing repo
Terminal window git clone https://github.com/octocat/Hello-World.gitcd Hello-Worldgit remote -v # See origin -
Add a remote to existing repo
Terminal window cd ~/learn-git# Create a repo on GitHub first, then:git remote add origin git@github.com:YOU/learn-git.gitgit remote -v -
Push your work
Terminal window git push -u origin main # -u sets upstreamgit push # Future pushes are simple -
Pull changes
Terminal window # After someone else pushes:git fetch # Download without merginggit status # See "behind by N commits"git pull # Fetch and merge -
Track remote branches
Terminal window git branch -r # List remote branchesgit switch -c feature origin/feature # Track remote
Checkpoint
Section titled “Checkpoint”Clone a repo. Push a new branch. Pull changes from origin.
Lesson 5: Rewriting History
Section titled “Lesson 5: Rewriting History”Goal: Clean up commits before sharing.
Concepts
Section titled “Concepts”Local commits can be modified. Published commits should not be rewritten (breaks collaborators). Use rebase and amend for unpushed work.
Exercises
Section titled “Exercises”-
Amend the last commit
Terminal window # Forgot a file:git add forgotten-file.txtgit commit --amend --no-edit# Fix the message:git commit --amend -m "Better message" -
Interactive rebase
Terminal window # Reword, squash, or reorder last 3 commitsgit rebase -i HEAD~3# In editor:# pick abc123 First commit# squash def456 Fixup commit <- squash into previous# reword ghi789 Bad message <- edit this message -
Rebase onto main
Terminal window git switch featuregit rebase main # Replay feature on top of main# Resolve conflicts if neededgit rebase --continue -
Abort a rebase
Terminal window # If it goes wrong:git rebase --abort
Checkpoint
Section titled “Checkpoint”Squash 3 commits into 1. Rebase a feature branch onto updated main.
Lesson 6: Undoing Things
Section titled “Lesson 6: Undoing Things”Goal: Recover from mistakes.
Concepts
Section titled “Concepts”| Situation | Command |
|---|---|
| Unstage a file | git restore --staged file |
| Discard local changes | git restore file |
| Undo last commit (keep) | git reset --soft HEAD~1 |
| Undo last commit (lose) | git reset --hard HEAD~1 |
| Revert a pushed commit | git revert <sha> |
| Find lost commits | git reflog |
Exercises
Section titled “Exercises”-
Unstage and restore
Terminal window echo "mistake" >> README.mdgit add README.mdgit restore --staged README.md # Unstagegit restore README.md # Discard changes -
Reset commits
Terminal window # Make a bad commitecho "oops" > oops.txtgit add oops.txt && git commit -m "Oops"git reset --soft HEAD~1 # Undo commit, keep stagedgit status # oops.txt still stagedgit reset HEAD oops.txt # Unstagerm oops.txt -
Revert a pushed commit
Terminal window git revert HEAD # Creates a new commit undoing HEADgit log --oneline # See revert commit -
Recover with reflog
Terminal window git reflog # History of HEAD movementsgit reset --hard abc123 # Go back to any state
Checkpoint
Section titled “Checkpoint”Accidentally reset —hard, then recover using reflog.
Lesson 7: Stash and Cherry-Pick
Section titled “Lesson 7: Stash and Cherry-Pick”Goal: Flexible workflows for real situations.
Exercises
Section titled “Exercises”-
Stash work in progress
Terminal window # Working on something, need to switch branchesecho "wip" >> README.mdgit stash # Save and cleangit switch other-branch# Do work...git switch maingit stash pop # Restore WIP -
Manage multiple stashes
Terminal window git stash listgit stash save "description"git stash apply stash@{1} # Apply without removinggit stash drop stash@{0} # Remove from list -
Cherry-pick a commit
Terminal window # Grab one commit from another branchgit log other-branch --onelinegit cherry-pick abc123 -
Find bug with bisect
Terminal window git bisect startgit bisect bad # Current is brokengit bisect good v1.0 # This version worked# Git checks out middle commit# Test it, then:git bisect good # or bad# Repeat until git finds the culpritgit bisect reset
Checkpoint
Section titled “Checkpoint”Stash changes, switch branches, pop the stash. Cherry-pick one commit.
Lesson 8: Workflows
Section titled “Lesson 8: Workflows”Goal: Understand team collaboration patterns.
Concepts
Section titled “Concepts”| Workflow | Description |
|---|---|
| Feature branch | Branch per feature, merge to main |
| GitHub Flow | Branch, PR, review, merge |
| Trunk-based | Short-lived branches, frequent integration |
| Gitflow | develop/release/hotfix branches (heavyweight) |
Exercises
Section titled “Exercises”-
Feature branch workflow
Terminal window git switch -c feature/add-search# Work...git push -u origin feature/add-search# Open PR on GitHub# After review, merge and delete branch -
Keep branch updated
Terminal window git switch feature/add-searchgit fetch origingit rebase origin/main # Or mergegit push --force-with-lease # Safe force push -
Clean up merged branches
Terminal window git branch --merged main | grep -v main | xargs git branch -dgit fetch --prune # Remove stale remote refs
Checkpoint
Section titled “Checkpoint”Complete a full cycle: branch → commits → push → PR → merge → delete branch.
Practice Projects
Section titled “Practice Projects”Project 1: Solo Workflow
Section titled “Project 1: Solo Workflow”Create a repo. Make 10 commits across 3 branches. Practice rebasing one branch onto another. Squash related commits. Push to GitHub.
Project 2: Collaboration Simulation
Section titled “Project 2: Collaboration Simulation”Clone your own repo to a second directory (simulating a collaborator). Make conflicting changes in both. Push from one, pull and resolve in the other.
Project 3: Archaeology
Section titled “Project 3: Archaeology”Clone a large open-source project. Use git log, git blame, and git bisect
to understand how a feature was implemented.
Command Reference
Section titled “Command Reference”| Stage | Must Know |
|---|---|
| Beginner | init add commit status log diff |
| Branching | branch switch merge rebase |
| Remote | clone remote push pull fetch |
| Undoing | restore reset revert reflog |
| Power | stash cherry-pick bisect rebase -i |
See Also
Section titled “See Also”- Git Cheatsheet — Quick command reference
- Problem Solving — Debugging git issues
- GitHub