🐍 Python for Everyone

← Back to the quiz

Chapter 29 Exercises 40 min, hands-on

Exercises: GitHub & Collaboration

How to run these Unlike most chapters, there's no Python function to write here -- the "code" is real git commands, run by hand in your own terminal. Download starter.py below, run python3 starter.py from the folder you saved it in, and it builds a real scenario/ folder on disk: a bare repository standing in for a GitHub remote, plus a working clone already connected to it. Every task below happens inside that scenario/my-local-work/ folder.
What starter.py builds

This is a description of the real folder structure starter.py creates on disk -- not a GitHub screenshot.

scenario/
├── upstream-remote.git/     <- real bare repo (git init --bare),
│                               standing in for "the project on GitHub"
└── my-local-work/           <- your working clone
    ├── README.md
    ├── app.py
    └── (origin already points at ../upstream-remote.git,
         and it starts ONE COMMIT BEHIND origin/main --
         a teammate "pushed" before you looked)

1 Confirm your remote

cd into scenario/my-local-work and run git remote -v. Confirm origin points at ../upstream-remote.git for both fetch and push.

2 Fetch, and prove it doesn't touch your branch

Run git fetch origin, then compare git log --oneline (local main) against git log --oneline origin/main. Confirm local main is unchanged even though origin/main moved.

3 Pull, and watch your branch actually update

Run git pull origin main, then git log --oneline again. This time local main fast-forwards to match origin/main.

🐞 Task 4 — Debug the Code: your push was rejected

After Task 3, a second teammate pushes another change directly to upstream-remote.git. Meanwhile, without fetching first, you make your own local commit and try to push it:

terminal
$ git push origin main
To ../upstream-remote.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '../upstream-remote.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.

Diagnose it, then fix it for real:

This exact sequence (rejection, fetch, graph, rebase, conflict, resolution, successful push) was verified for real while building this exercise — run python3 solution.py to see it happen end to end, with real command output at every step.

← Back to Quiz Practice Bank →