Fetch, pull and push
Move commits between your repository and a remote: fetch to look, pull to integrate, push to publish, and what to do when a push is rejected.
Remotes, clone and originMerging: fast-forward and three-wayRebasing a branch
Fetch, pull and push
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `git fetch` downloads new commits and moves `origin/main` but never touches your branches or working tree.
- `git pull` is `git fetch` followed by a merge (or a rebase) of the upstream branch into your current branch.
- `git push` is refused with `non-fast-forward` when the remote has commits you don't, and the fix is to integrate first, not to force.
- Newer Git refuses to guess how to pull when your branch and the remote have diverged, so choose `--rebase`, `--no-rebase` or `--ff-only`.
- `git status -sb` only knows what your last fetch saw, so it can say you are up to date when the remote has moved.
Two copies drift apart
The moment a second person, or a second laptop, has a copy, histories drift. Your teammate adds a commit to the remote while you add one locally. Git needs three separate verbs for the three jobs: look at what changed, bring it into your work, and publish yours.
Every output below comes from a real run on Git 2.54 using the jot repository from the last lesson, with ../hub.git as the remote. Your hashes will differ.
Fetch: look without touching
A teammate pushed a commit. Before fetching, my repository has no idea:
$ git status -sb
## main...origin/main
$ git fetch
From ../hub
5cf38a4..fd56fb7 main -> origin/main
$ git status -sb
## main...origin/main [behind 1]
$ git log --oneline main..origin/main
fd56fb7 Add TODO
$ git diff --stat main origin/main
TODO | 1 +
1 file changed, 1 insertion(+)
The first status said I was in sync because it only compares against my local origin/main. After the fetch, that bookmark moved, and Git can say “behind 1”. main..origin/main reads “commits reachable from origin/main but not from main”, which is the incoming list. My branch and files didn’t change at all. Fetch is always safe.
Pull: fetch plus integrate
git pull is two commands in a trench coat. When your branch simply hasn’t moved, the merge half is a fast-forward:
$ git pull
Updating 5cf38a4..fd56fb7
Fast-forward
TODO | 1 +
create mode 100644 TODO
Trouble starts when both sides moved. I committed a README change, my teammate pushed a TODO change, and now:
$ git status -sb
## main...origin/main [ahead 1, behind 1]
$ git pull
hint: You have divergent branches and need to specify how to reconcile them.
...
fatal: Need to specify how to reconcile divergent branches.
Git used to quietly create a merge commit here. It now makes you choose, once per pull or permanently:
| Choice | Command | Result |
|---|---|---|
| Merge | git pull --no-rebase |
A merge commit joins the two lines |
| Rebase | git pull --rebase |
Your commits are replayed on top of the remote’s |
| Refuse | git pull --ff-only |
Succeeds only if no integration is needed |
Which one is right is the question of merge or rebase. Many people set git config --global pull.rebase true or pull.ff only and stop thinking about it. --ff-only is the cautious choice, because it turns “I’m not sure” into an error you can read.
Push: publish, if it is a fast-forward
Before integrating, I tried to push my diverged commit:
$ git push
To ../hub.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to '../hub.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. ...
A push moves the remote’s branch to point at your commit. Git allows that only when your commit already contains everything the remote branch has, which is a fast-forward. Otherwise the move would orphan your teammate’s commit. The fix is to integrate, then push:
$ git pull --rebase
Rebasing (1/1)Successfully rebased and updated refs/heads/main.
$ git push
To ../hub.git
59ba0f1..c1a3b1d main -> main
Rebasing replayed my one commit after the teammate’s, so the history stayed linear and the push became a fast-forward. Resist --force here. Force pushing safely covers the cases where overwriting remote history is legitimate, and it is not this one.
A habit that prevents most problems
git fetch first, then look, then decide. Running git fetch and then git log --oneline --graph --all costs nothing and shows both histories side by side before you commit to a merge or a rebase.
What the video covers
The Modern Coder’s “Git Pull vs Fetch: When To Use Each” is about seven minutes, from around 2024. From its title and description, it compares the two commands and when to prefer each. I judge English from the title, description and channel only, and I could not read a transcript, so I can’t say whether it covers the divergent-branch error above. The channel’s “Git CLONE, PUSH & FETCH Tutorial” is linked as a second view.
Where this fits
You can now exchange commits. The next lesson explains the link that makes a bare git push or git pull know where to go: upstream tracking branches.
Check yourself
What is the difference between `git fetch` and `git pull`?
`git fetch` only updates your remote-tracking branches. `git pull` fetches and then merges or rebases the result into your current branch.
`git status` says your branch is up to date with `origin/main`. Can the remote still have new commits?
Yes. The comparison is against your local `origin/main`, which only changes when you fetch. Run `git fetch` first for an accurate answer.
Your push is rejected as non-fast-forward. What does that mean?
The remote branch contains commits that your branch doesn't, so moving the remote branch to yours would drop them. Integrate the remote's commits first, then push.
When would you use `git pull --ff-only`?
When you want the pull to succeed only if it can move your branch forward cleanly. If the branches diverged it stops, so you decide how to combine them.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.