Exercises: GitHub & Collaboration
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.
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:
$ 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:
- Run
git fetch origin, thengit log --oneline --graph --allto see exactly how the history diverged. - Run
git rebase origin/mainto replay your commit on top of the teammate's. If both changes touched the same lines, resolve the resulting conflict the same way Chapter 28 taught (edit out the conflict markers,git addthe file, thengit rebase --continue). - Push again — it now succeeds.
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.