Exercises — Module 1: Git
How to use
do these in order. Don't copy-paste blindly — type the commands. If something surprises you, that's the lesson. Checkpoints (✓) tell you how to verify yourself.
Part A — Warm-Up Drills (30–45 min)
Create a sandbox: mkdir git-drills && cd git-drills && git init
Drill 1 — The three areas.
- Create
app.txtwith one line. Rungit status— which area is it in? git add app.txt, rungit statusagain. Which area now?- Commit it. Modify the file, run
git diff, thengit add, thengit diff(nothing?) andgit diff --staged(there it is). ✓ You can explain whygit diffshowed nothing after staging.
Drill 2 — Ten-commit history. Make 10 commits (change a file each time, proper conventional messages: feat:, fix:, docs:...).
✓ git log --oneline shows 10 clean, meaningful messages.
Drill 3 — Inspection.
git log --oneline --graphgit show HEAD~3— what changed 3 commits ago?git blame app.txt— which commit last touched line 1? ✓ You can find "who changed what, when" without Google.
Drill 4 — Branch pointer intuition.
git branch test(create but don't switch).git log --oneline -1 test— same commit as main?- Commit on main. Where does
testpoint now? Why didn't it move? ✓ You can explain "a branch is just a pointer" using this experiment.
Part B — Guided Labs
Lab 1 — The Feature Branch Workflow (45 min)
- Create a GitHub repo
git-practice, clone it. - Create branch
feature/greeting, addgreet.shthat echoes hello, commit, push with-u. - Open a Pull Request on GitHub. Read the diff view like a reviewer would.
- On GitHub, leave a comment on your own line of code, then push a second commit addressing it. Watch the PR update.
- Merge the PR. Locally:
git switch main && git pull. ✓git log --onelineon main shows your feature commits + merge commit. - Enable branch protection on main (require PR). Try
git pusha direct commit to main. ✓ It gets rejected — you've experienced the guardrail you'll later configure for teams.
Lab 2 — Manufacture and Resolve a Conflict (30 min)
- From main, create
branch-a; change line 1 ofapp.txtto "version A", commit. - Switch to main, create
branch-b; change line 1 to "version B", commit. - Merge
branch-ainto main (clean). Now mergebranch-b— conflict! - Open the file, study the
<<<<<<<markers. Resolve keeping BOTH lines, complete the merge. ✓git log --graph --onelineshows the diamond shape. - Repeat the whole exercise, but resolve using
git merge --abortinstead. When would you do that in real life?
Lab 3 — Undo Everything (45 min) — the confidence builder
- Make 3 commits: C1, C2, C3.
git reset --soft HEAD~1— where did C3's changes go? (git status)- Re-commit. Now
git reset --hard HEAD~2. Confirm C2, C3 gone fromgit log. - Recover them:
git reflog, find C3's hash,git reset --hard <hash>. ✓ All 3 commits back. Say out loud: "committed work is almost never lost." - Push everything. Now
git revert HEADand push. ✓ History shows both the bad commit and its revert — explain why this is the ONLY safe undo on shared branches.
Lab 4 — Rebase & Interactive Rebase (45 min)
- Create
feature/xfrom main; make 2 commits. Switch to main, make 1 commit (simulating a teammate). - On
feature/x:git rebase main. Comparegit log --graphbefore/after (screenshot or copy both). ✓ You can draw what happened with commit letters. - Make 4 messy commits ("wip", "fix typo", "actually fix", "final").
git rebase -i HEAD~4, squash into one well-named commit. ✓git logshows one clean commit. This is your pre-PR cleanup ritual.
Part C — Challenges (no instructions — success criteria only)
C1. Your repo has a file secret.txt committed 5 commits ago. Prove the "deleting it now isn't enough" claim: delete it, commit, then show a command that still displays its contents from history.
C2. Cherry-pick: create branch release/1.0 from an old commit; a bug fix lands on main; bring ONLY that one commit to the release branch without merging anything else.
C3. Stash flow: start editing a file, then (without committing) switch to fix an urgent "bug" on another branch, come back, and resume exactly where you were.
C4. Bisect: create 20 commits where commit ~13 introduces the word "BUG" into app.txt. Use git bisect with grep -q BUG app.txt && exit 1 || exit 0 logic to find the guilty commit in ≤5 steps.
C5. Write a .gitignore for a Python project, then prove it works: git status must not show __pycache__/, .env, or .venv/ after you create them.
Part D — Break & Fix Scenarios (simulated incidents)
D1 — "I committed to the wrong branch." Make 2 commits on main that belonged on a feature branch. Move them to a new branch and reset main — without losing anything. (Hint: branch first, reset after.)
D2 — "The rebase went wrong." Start an interactive rebase, delete a commit you needed, complete it. Now recover the lost commit via reflog.
D3 — "Detached HEAD panic." git checkout <old-hash>, make a commit there, switch back to main. Your commit is now orphaned — find it and save it to a branch.
D4 — Force-push disaster drill (sandbox only!). Clone your repo twice (as "you" and "teammate"). You rebase+force-push main; teammate pulls. Observe and document the mess. Now write, in 3 sentences, the rule you'll follow forever.
Part E — Interview Rehearsal (answer OUT LOUD, 2–3 min each)
- Walk me through your team's Git workflow from branch to production.
mergevsrebase— when each, and why never rebase shared branches?- A secret was pushed to GitHub. What do you do, in order?
- Explain
reset --softvs--mixedvs--hardwith one use case each. - How would you find which commit broke the build last week?
Done when
every ✓ passed, C1–C5 solved without looking back at the module, and you can answer Part E without notes.
