Lesson 3 of 5 in Remotes & Collaboration

Fetching and pulling

Story: The Partner keeps adding to the archive. See what's new before you build on it.

Your teammates push too. Their commits reach your branch in two steps: download them, then add them to your branch.

Fetch: download

git fetch

git fetch asks origin what's new and downloads it. It moves your remote-tracking branches, such as origin/main, to where those branches are on origin now. It never changes your own branches or your files, so it's always safe to run.

After a fetch, git status can tell you how your branch compares:

Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.

Before you take the new commits in, you can look at them: git log --oneline --all shows them on origin/main.

Merge: add them to your branch

origin/main is a branch like any other, so you merge it like any other:

git merge origin/main

If you haven't committed since your branch and origin/main last matched, this is a fast-forward: main just moves up to origin/main.

Pull: both at once

git pull fetches, then merges your branch's upstream into it. On main, it's git fetch followed by git merge origin/main. Most of the time, that's all you need.

When both sides have new commits

Sometimes you've committed on main and a teammate has pushed to main too. Your main and origin/main have diverged: each has a commit the other doesn't. A fast-forward can't take in both. git can join them with a merge commit, or it can rebase: replay your commits on top of theirs. Rewriting History covers rebase.

git 2.45 won't guess which you want. Until you've told it, a pull on diverged branches fetches, then stops and asks you to choose:

hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
...
fatal: Need to specify how to reconcile divergent branches.

Nothing has been merged yet, and your files are as they were. The fetch did happen, though, so origin/main already has their commits. Choose merging, for every repository you use:

git config --global pull.rebase false

--global sets it once for all your repositories. Without it, the setting applies only to the repository you're in. Then pull again: git merges, and makes a merge commit whose parents are your commit and theirs.

You only set this once. From then on, a pull on diverged branches merges, and the projects in the rest of this course already have it set.

Uncommitted changes

A pull that would change a file you've edited but not committed stops before it merges:

error: Your local changes to the following files would be overwritten by merge:
	index.html
Please commit your changes or stash them before you merge.
Aborting

Your edit and your branch are just as they were, though the fetch did happen. Commit your edit, then pull again. git's other suggestion, stashing, works too.

Fetch or pull?

What happens git fetch git pull
Downloads new commits yes yes
Moves origin/main yes yes
Changes your branch and your files no yes: it merges

Fetch when you want to look first. Pull when you're ready to take the work in. And pull before you push: while origin has commits you don't, your push is refused.

New commands

  • git fetch
  • git pull
  • git config pull.rebase

Explainer: Fetch downloads, merge brings it in

project: your repo, you are hereorigin: remote host (bare), https://git.example/team/project.git99860cd Add a README99860cdd1c0363 Add the home paged1c03634607830 Style the heading4607830de1d2f7 Add the opening hoursde1d2f719916ce Add a menu page19916ce65539bc Merge remote-tracking branch 'origin/main'65539bcHEAD → mainorigin/HEADorigin/main99860cd Add a README99860cdd1c0363 Add the home paged1c03634607830 Style the heading460783019916ce Add a menu page19916cemain
The repository at the end of the Explainer.
Graph as text

project: your repo, you are here

  • HEAD → main at 65539bc "Merge remote-tracking branch 'origin/main'"

Branches and tags

  • main at 65539bc "Merge remote-tracking branch 'origin/main'", tracks origin/main
  • origin/HEAD at 19916ce "Add a menu page"
  • origin/main at 19916ce "Add a menu page"

Commits, newest first

  1. 65539bc "Merge remote-tracking branch 'origin/main'"; parents de1d2f7 and 19916ce; refs: HEAD → main
  2. 19916ce "Add a menu page"; parent 4607830; refs: origin/HEAD, origin/main
  3. de1d2f7 "Add the opening hours"; parent 4607830
  4. 4607830 "Style the heading"; parent d1c0363
  5. d1c0363 "Add the home page"; parent 99860cd
  6. 99860cd "Add a README"; no parent

origin: remote host (bare), https://git.example/team/project.git

Branches and tags

  • main at 19916ce "Add a menu page"

Commits, newest first

  1. 19916ce "Add a menu page"; parent 4607830; refs: main
  2. 4607830 "Style the heading"; parent d1c0363
  3. d1c0363 "Add the home page"; parent 99860cd
  4. 99860cd "Add a README"; no parent

Not shown: teammate.

Challenge 1 of 3

Story: Someone's been in the archive. Find out what changed before you touch anything.

A teammate has pushed two new commits to main on origin. Download them into your repository so you can look them over, but don't change your main or your files yet.

Unlocks after Branching & Merging.

Terminal

Unlocks after Branching & Merging.

This Lesson can be played once Branching & Merging is complete. Its text and Explainer are open to everyone.