Skip to content
devops.academy

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.

  1. Create app.txt with one line. Run git status — which area is it in?
  2. git add app.txt, run git status again. Which area now?
  3. Commit it. Modify the file, run git diff, then git add, then git diff (nothing?) and git diff --staged (there it is). ✓ You can explain why git diff showed 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 --graph
  • git 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.

  1. git branch test (create but don't switch). git log --oneline -1 test — same commit as main?
  2. Commit on main. Where does test point 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)

  1. Create a GitHub repo git-practice, clone it.
  2. Create branch feature/greeting, add greet.sh that echoes hello, commit, push with -u.
  3. Open a Pull Request on GitHub. Read the diff view like a reviewer would.
  4. On GitHub, leave a comment on your own line of code, then push a second commit addressing it. Watch the PR update.
  5. Merge the PR. Locally: git switch main && git pull. ✓ git log --oneline on main shows your feature commits + merge commit.
  6. Enable branch protection on main (require PR). Try git push a 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)

  1. From main, create branch-a; change line 1 of app.txt to "version A", commit.
  2. Switch to main, create branch-b; change line 1 to "version B", commit.
  3. Merge branch-a into main (clean). Now merge branch-b — conflict!
  4. Open the file, study the <<<<<<< markers. Resolve keeping BOTH lines, complete the merge. ✓ git log --graph --oneline shows the diamond shape.
  5. Repeat the whole exercise, but resolve using git merge --abort instead. When would you do that in real life?

Lab 3 — Undo Everything (45 min) — the confidence builder

  1. Make 3 commits: C1, C2, C3.
  2. git reset --soft HEAD~1 — where did C3's changes go? (git status)
  3. Re-commit. Now git reset --hard HEAD~2. Confirm C2, C3 gone from git log.
  4. 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."
  5. Push everything. Now git revert HEAD and 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)

  1. Create feature/x from main; make 2 commits. Switch to main, make 1 commit (simulating a teammate).
  2. On feature/x: git rebase main. Compare git log --graph before/after (screenshot or copy both). ✓ You can draw what happened with commit letters.
  3. Make 4 messy commits ("wip", "fix typo", "actually fix", "final"). git rebase -i HEAD~4, squash into one well-named commit. ✓ git log shows 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)

  1. Walk me through your team's Git workflow from branch to production.
  2. merge vs rebase — when each, and why never rebase shared branches?
  3. A secret was pushed to GitHub. What do you do, in order?
  4. Explain reset --soft vs --mixed vs --hard with one use case each.
  5. 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.