Sub topic 6
Reviewing what fetch brought in
This chapter assumes you can read git log output, know what a branch is, and know that git fetch updates remote-tracking branches such as origin/main without touching your working tree. Here you will learn how to inspect what a fetch downloaded before you merge or rebase it.
Why inspect first
After git fetch origin, nothing in your checked-out files has changed. The new commits sit behind origin/main, and your local main still points where it did. This gives you a chance to look before you integrate. Two commands do most of the work: git log for commits and git diff for content.
Seeing which commits you lack
The range main..origin/main means "commits reachable from origin/main but not from main." That is exactly the incoming work.
git fetch origin
git log --oneline main..origin/mainIf the output is empty, the remote has nothing new for you. To see who wrote each commit and which files it touched, add --stat:
git log --stat main..origin/mainThe reverse range, origin/main..main, lists your local commits that are not yet pushed. Checking both ranges tells you whether you are behind, ahead, or diverged.
| Range | Shows | Non-empty means |
|---|---|---|
| `main..origin/main` | Commits only on the remote | You are behind |
| `origin/main..main` | Commits only on your branch | You are ahead |
| `main...origin/main` | Commits on either side, not both | Use with `--left-right` to tell them apart |
Seeing what changed
Use git diff to read the actual changes. The three-dot form compares origin/main against the common ancestor of the two branches.
git diff main...origin/mainThis shows what the remote changed since your branches diverged, and it leaves out your own local work. The two-dot form, git diff main origin/main, compares the two tips directly. If you have local commits, that output mixes their reversal with the remote's changes, which is usually confusing.
For a quick summary, use --stat or --name-only:
git diff --stat main...origin/main
git diff --name-only main...origin/mainTelling the sides apart
When history has diverged, --left-right marks each commit with < for your side and > for the remote side.
git log --oneline --left-right main...origin/main< 4c1d9e2 Fix typo in README
> a83f0b7 Add rate limiting
> 91be3c4 Update dependenciesHere you have one local commit and the remote has two. A plain git pull would need to merge or rebase, so you now know to choose a strategy deliberately.
Looking at a single commit
You can also inspect one incoming commit by name, or check out the remote branch in detached mode to try it out.
git show a83f0b7
git switch --detach origin/mainDetached mode lets you run tests against the remote state. Go back with git switch main.
Common errors
- Empty output right after someone says they pushed. You forgot to run
git fetch. The remote-tracking branch only updates when you fetch. - Using two dots with
git diffand seeing your own changes reversed. Use three dots to see only the remote's changes. - Comparing against
mainwhen you meant a different branch. Checkgit branch -vvto see which upstream your branch tracks.
Exercise
You ran git fetch origin. Which command lists only the commits on origin/main that are not in your local main, and which command shows the file changes the remote made since your branches diverged?
Show answer
git log --oneline main..origin/main lists the incoming commits. git diff main...origin/main (three dots) shows the remote's changes since the common ancestor.