🐍 Python for Everyone

← Back to the quiz

Chapter 28 Exercises 45 min, hands-on

Exercises: Git Fundamentals

How to run these This chapter has no Python functions to write. Download starter.py below and run python3 starter.py from inside the folder you saved it in — it builds a real git repository in a new git-playground/ folder, with a commit history already in place. From there, every task below is a real git command you run in your own terminal against that repository.

1 Read the history

Run git log --oneline --all --graph inside git-playground/. Find exactly where main and feature/priority diverged.

2 Check status and diff first

starter.py left an uncommitted change in tasks.py on purpose. Run git status, then git diff, and read exactly what changed before doing anything else.

3 Discard the uncommitted change

Use the correct command to throw away that uncommitted change entirely, restoring tasks.py to its last committed state.

4 Trigger and resolve a real merge conflict

Run git merge feature/priority from main. It will conflict. Hand-resolve tasks.py's conflict markers into one correct add_task() that keeps both branches' changes, then git add + git commit to finish the merge.

tasks.py (mid-conflict)
<<<<<<< HEAD
def add_task(tasks, description):
    if not description.strip():
        raise ValueError("description cannot be empty")
    tasks.append({"description": description, "done": False})
=======
def add_task(tasks, description, priority="normal"):
    tasks.append({"description": description, "done": False, "priority": priority})
>>>>>>> feature/priority
    return tasks

5 Verify your resolution actually works

A conflict resolution that "looks right" isn't the same as one that is right. Run the verification snippet from solution.py's Task 5 comment against your resolved tasks.py and confirm the output matches exactly.

6 Recover the "lost" commit with reflog

starter.py committed a task_count() function and then immediately hard-reset it away, so it's not in git log anymore. Use git reflog to find its hash and bring it back.

7 Stash a change, then bring it back

Add a small new function to tasks.py without committing it. Use git stash to shelve it, confirm the working tree is clean, then use git stash pop to bring it back exactly as it was.

🐞 Task 8 — Debug the Situation

A teammate says: "I ran git reset --hard on the wrong branch and now my last three commits are just gone from git log — is my work actually lost?"

Their mistake is assuming git reset --hard deletes commits outright. Explain what actually happened to those three commits, and give the exact command sequence that recovers them.

💼 Scenario Task 9 — The production hotfix

You're two days into a messy, half-broken feature branch when a production bug needs an urgent fix based on the current stable code, not your in-progress feature. Walk through, command by command, exactly what you'd run to fix and ship the hotfix without touching or losing any of your feature branch's in-progress work.

Checking your work There's no single "correct" final state to diff against, since this chapter is about the git commands themselves. After Task 4, your merged tasks.py should behave exactly as Task 5's check expects. After Task 6, git log --oneline should show the recovered commit again. After Task 7, git stash list should be empty again. Every command's exact output in solution.py was verified against this course's own build system before being written down — if your output differs meaningfully (not just commit hashes, which are always unique), re-read the relevant lesson sub-topic.
Cleaning up git-playground/ is disposable practice material, not something to keep. When you're done: cd .. then rm -rf git-playground. Running python3 starter.py again always rebuilds the exact same starting point, so you can redo any task as many times as you want.