Lesson 2 · Git workflow in the age of agents

Your laptop has a bookmark, not a live view

Three copies of main, and which command moves which. About 25 minutes.

Today’s win: before you run fetch, pull, or push, you’ll be able to say what it will move, and you’ll catch git status telling you something out of date.

Three mains, not one

In Lesson 1 you saw origin/main in your log and moved on. Here’s what it is. There are really three things called “main”:

  1. main on GitHub: the real shared branch. It moves whenever anyone pushes or merges a PR, including an agent.
  2. origin/main on your laptop: a remote-tracking branch. It’s Git’s note of where GitHub’s main was the last time you checked.
  3. main on your laptop: your own branch. It moves when you commit, pull, or merge.

“Think of them as bookmarks, to remind you where the branches in your remote repositories were the last time you connected to them.” Quote and the next one: Pro Git §3.5, “Remote Branches”. The book also says these are “local references that you can’t move; Git moves them for you whenever you do any network communication.”

The key detail is that a bookmark doesn’t update itself: “as long as you stay out of contact with your origin server, your origin/master pointer doesn’t move.” (master is the book’s older name for main.)

“git status tells me whether I’m up to date with GitHub.” It doesn’t. It compares your main with your bookmark origin/main, and it never contacts GitHub. So “Your branch is up to date with ‘origin/main’” can be true while GitHub has ten new commits.

Which command moves which

CommandTalks to GitHub?Moves
git commitNoyour main (the branch HEAD is on)
git fetchYes, downloadsorigin/main only. Your files and your main are untouched.
git pullYes, downloadsgit fetch, then brings origin/main into your main
git pushYes, uploadsGitHub’s main, and your origin/main with it

fetch “will not modify your working directory at all” (Pro Git §3.5). pull “runs git fetch … Then it integrates that branch into the current branch” (git-pull docs). So pull isn’t a separate kind of download. It’s a fetch with an integration step added.

Play with it first

Click the buttons and watch which box changes. Newly arrived commits glow. Violet commits are your teammate’s and blue ones are yours. Before each click, predict which box will change and what the terminal will say.

Stuck on challenge 3?

A rejected push means GitHub has commits that your main doesn’t contain. You have to bring them in first. Plain git pull refuses when both sides have new commits. On git 2.39 that’s the “Need to specify how to reconcile divergent branches” error. git pull --no-rebase tells it to merge, which creates a merge commit (the square M) that contains both sides. Then the push goes through. Lessons 3 and 6 cover what happens when both sides change the same lines, and the rebase alternative. Default behaviour: “git pull --ff-only … fails if your local branch has diverged from the remote branch. This is the default.” git-pull docs.

Agents make this matter more. A cloud agent or a teammate’s Claude Code session can push branches and merge PRs while you aren’t looking, and your laptop learns about it only when you fetch. Before you review an agent’s branch locally, or start new work from main, sync first. The habit is git switch main, then git pull.

Do it for real

Back to ~/Work/git-sandbox. To play the teammate, you’ll make a second clone of the same GitHub repo. It stands in for a colleague’s laptop, or an agent working somewhere else.

  1. Find the stale bookmark left over from Lesson 1
    cd ~/Work/git-sandbox
    git branch -a

    You should still see remotes/origin/add-greeting, even though that branch was deleted on GitHub. Predict why before reading on. A bookmark changes only when you talk to the server, and a plain fetch doesn’t remove bookmarks for deleted branches. Clean it up:

    git fetch --prune
    git branch -a

    --prune: “Before fetching, remove any remote-tracking references that no longer exist on the remote.” git-fetch docs. Optional: git config --global fetch.prune true makes every fetch do this.

  2. Become the teammate: clone, then run the whole PR loop from memory
    cd ~/Work
    gh repo clone git-sandbox git-sandbox-teammate
    cd git-sandbox-teammate

    Now, without opening Lesson 1, take one change through the full loop. Make a branch, add a file teammate.txt, commit, push, open a PR, and merge it with branch deletion. Recalling the commands is part of the practice.

    Check your commands (only after trying)
    git switch -c add-teammate-note
    echo "Hi from your teammate" > teammate.txt
    git add teammate.txt
    git commit -m "Add teammate note"
    git push -u origin add-teammate-note
    gh pr create --fill
    gh pr merge --merge --delete-branch
  3. Go back to your clone and ask git status
    cd ~/Work/git-sandbox
    git status

    GitHub’s main just moved. Predict what git status says, then run it. It says “up to date with ‘origin/main’”, because your bookmark hasn’t moved and status didn’t ask GitHub.

  4. Fetch, and watch the bookmark move
    git fetch
    git status
    git log --oneline --graph --all

    Predict the number before you run it: behind by how many commits? It’s 2: the teammate’s commit and the merge commit GitHub created. In the log, origin/main is now above (HEAD -> main). Run ls and notice that teammate.txt isn’t there yet. Fetch doesn’t touch your files.

  5. Pull, and catch up
    git pull
    git log --oneline --graph --all
    ls

    The output says Fast-forward. Your main had no new commits of its own, so Git only slid the label forward to where origin/main is. Nothing needed merging. Now teammate.txt is there.

  6. Be “ahead”: commit on a branch before pushing
    git switch -c add-notes
    echo "Remotes are bookmarks" > notes.txt
    git add notes.txt && git commit -m "Add notes"
    git push -u origin add-notes
    echo "fetch moves origin/*, pull also merges" >> notes.txt
    git commit -am "Expand notes"
    git status

    The status reads “ahead of ‘origin/add-notes’ by 1 commit”: your second commit exists only on your laptop. git push sends it and moves both GitHub’s branch and your bookmark. Run git status again to see “up to date”.

  7. Finish the loop
    git push
    gh pr create --fill
    gh pr merge --merge --delete-branch
    git switch main
    git pull
    git fetch --prune
    git log --oneline --graph --all

Close the feedback loop: paste the final git log --oneline --graph --all into Claude Code. You should see two more merge commits than last time, with HEAD -> main and origin/main on the same commit. I’ll walk through anything that looks off.

Recall it

No scrolling up. Try this again in two or three days, and also retry the Lesson 1 quiz. Spaced retrieval builds long-term memory.

Go deeper

Primary source: Pro Git §3.5, “Remote Branches”. Read up to “Tracking Branches”. Its diagrams show the same three-box idea with master.

Optional extra practice: the Remote tab on Learn Git Branching, levels “Push & Pull” 1–4.

Reference: GitHub flow cheat sheet (now with a sync section) · Glossary.

Did something print output you didn’t expect, or are you wondering why pull doesn’t just always merge? Ask me in Claude Code. Questions like these are what I’m here for.

Related ideas