A local branch and its remote copy are separate refs. origin/main is a remote-tracking ref that records the last known state of the remote; it updates when you fetch, not automatically. Confusing the two is behind many “my branch is wrong” problems.
Before you start
You should be comfortable with branches and remotes. This article covers remote-tracking refs and synchronizing.
Step-by-step walkthrough
Step 1: Distinguish local and remote-tracking refs
main is your local branch; origin/main is a snapshot of the remote’s main from the last fetch. They can differ, and git status tells you how. Comparing against origin/main compares against the last fetch, not the live remote.
Step 2: Fetch updates without changing your work
git fetch downloads new objects and updates remote-tracking refs, leaving your working tree alone. git pull is fetch plus a merge or rebase into your current branch, so it changes your working tree. Fetch-then-inspect is the safer default because it separates download from integration.
Step 3: Use tracking branches
A local branch can track a remote branch, so git status shows ahead and behind counts and git push knows the default target. Setting upstream with -u makes the relationship explicit and avoids repeated remote arguments.
Worked scenario
Fetch updates the ref without touching the working tree.
git fetch origin
git log --oneline origin/main # inspect what arrived
git status # see ahead/behind vs origin/main
git pull --rebase # integrate if appropriateWalk through the example
git fetch brings down commits and moves origin/main forward, but your main is unchanged. You inspect what arrived, then decide whether to merge or rebase. git status compares your branch to origin/main, which is the snapshot from the last fetch.
Common mistake
Assuming git status compares to the live remote, when it compares to the last fetched snapshot. Another is running git pull on a branch with local changes and getting an unexpected merge commit.
Verify the behavior
Fetch and confirm origin/main moves while main stays. Compare git rev-parse main and git rev-parse origin/main. Push a commit and confirm git status reports the branch as ahead.
Interview exercise
What is the difference between git fetch and git pull?
Answer and reasoning
git fetch downloads new objects and updates remote-tracking refs without changing your working tree or current branch. git pull is git fetch followed by integrating the fetched changes into your branch, via merge or rebase, which does change your working tree. Fetch separates download from integration; pull combines them.
Continue learning
Compare integration in Merge vs rebase and branches in Branch strategies. Read the Git remote branches documentation and try the Git interview questions.