Fetch or pull? Get GitHub's changes safely

safe beginner #fetch#pull#sync

When to use this: GitHub may have newer commits (another PC, a merged PR) and you want them locally.

Before you start

Steps

  1. Look first (fetch)

    terminal
    git fetch
    git log HEAD..origin/main --oneline

    Fetch downloads but changes nothing in your files. The log shows what a pull would bring in.

  2. Take the changes (pull)

    terminal
    git pull

    Does fetch and then merges origin/main into your current branch. If you have no local commits it simply fast-forwards.

    Expected output
    Updating 3f2a1b9..8c4d7e0
    Fast-forward
     src/pages/index.astro | 4 ++--
  3. Strict option: refuse anything but a fast-forward

    terminal
    git pull --ff-only

    If your branch has diverged, this stops instead of creating a merge commit. Then you decide whether to merge or rebase deliberately.

How to verify it worked

  • git status -sb shows no behind.

How to undo it

If a pull merged something you did not want and you have not pushed, go back to where you were before the pull:

terminal
git reset --hard ORIG_HEAD

Common errors and fixes

fatal: Need to specify how to reconcile divergent branches Both sides have new commits. Choose: git pull --no-rebase (merge) or git pull --rebase. To make merge the default: git config --global pull.rebase false.
Your local changes would be overwritten by merge Commit or stash first (git stash push -u), then pull.

Q&A

Is git pull just git fetch plus something?

Yes: fetch followed by merge (or rebase if configured) of the upstream branch into your current branch.

Why would I ever fetch without pulling?

To inspect first. It lets you compare (git log HEAD..origin/main) without touching your files.

Practice these as flashcards →

Terms in this scenario

Related scenarios

My notes

Saved in this browser only.