100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Bash

Push, Pull, and Fetch

Push, pull, and fetch are the three commands that move commits between your local repository and a remote — understanding exactly what each does (and doesn't do) prevents most everyday Git confusion.

Working with RemotesBeginner8 min readJul 9, 2026
Analogies

Push, Pull, and Fetch

These three commands are how a distributed Git repository stays synchronized with a remote. git fetch downloads new commits, branches, and tags from a remote into your local remote-tracking branches, without touching your working tree or local branches at all. git push uploads your local commits to update a branch on the remote. git pull is a convenience command that runs a fetch followed immediately by a merge (or, if configured, a rebase) of the fetched branch into your current branch. Of the three, fetch is the safest — it never changes your working files — while pull and push both modify state and can, in the wrong circumstances, cause conflicts or overwrite work.

🏏

Cricket analogy: Fetch is like watching the opposition's practice session through binoculars without stepping onto the field — you see what they're doing, but nothing changes on your own side until you decide to act on it, unlike actually walking out to bat (push) or adjusting your game plan mid-innings (pull).

Fetch: Look Before You Leap

git fetch updates your remote-tracking branches (e.g. origin/main) to match the remote's current state, but your local main branch is untouched until you explicitly merge or rebase. This makes fetch a safe way to see what has changed upstream — run git log main..origin/main or git diff main origin/main afterward to preview incoming changes before deciding how to integrate them. Experienced Git users often prefer explicit fetch + merge/rebase over pull precisely because it separates 'see what's new' from 'apply it', giving them a chance to review first.

🏏

Cricket analogy: This is like checking a live scoreboard from another ground without it affecting your own team's actual innings — you can compare it to your team's current total and decide how to respond, but nothing about your own game changes until you act on what you saw.

Pull, and Push's Failure Modes

By default git pull performs a merge, creating a merge commit if your local branch has diverged from the remote branch. Running git pull --rebase instead replays your local commits on top of the fetched branch, producing a linear history without a merge commit — many teams set pull.rebase = true globally so this becomes the default behavior. If your working tree has uncommitted changes that conflict with incoming commits, pull will refuse to proceed (or, in the rebase case, may pause partway through with a conflict) until you commit, stash, or discard those local changes. On the push side, git push uploads your local branch's commits to the corresponding remote branch and by default fails with a 'non-fast-forward' rejection if the remote branch has commits your local branch doesn't have — this is Git protecting you from silently discarding someone else's work. The correct fix is usually to git pull (or fetch + rebase/merge) first, resolve any conflicts, and then push again. git push --force overrides this protection and overwrites the remote branch unconditionally; git push --force-with-lease is the safer variant, which only forces if the remote hasn't changed since your last fetch, refusing otherwise.

🏏

Cricket analogy: This is like a captain refusing to declare an innings closed (push rejection) if the scoreboard shows the opposition has already added runs he hasn't accounted for — he must first check the latest total (pull) and reconcile before declaring, or use a verified override (force-with-lease) only if nothing changed since his last check.

bash
# Safely inspect what's new upstream before merging
git fetch origin
git log --oneline main..origin/main

# Pull with rebase instead of merge, avoiding an extra merge commit
git pull --rebase origin main

# Push local commits on the current branch to origin
git push origin feature/checkout-redesign

# First push of a new branch: set it to track the remote branch
git push -u origin feature/checkout-redesign

# Rejected push (remote has commits you don't have)
# ! [rejected]  main -> main (fetch first)
# hint: Updates were rejected because the remote contains work you don't have locally.
git pull --rebase origin main
git push origin main

Avoid git push --force on shared branches like main or develop — it silently overwrites the remote history, potentially deleting commits your teammates already pushed. If a force push is genuinely necessary (e.g. after an interactive rebase on your own feature branch), use git push --force-with-lease and communicate the change to anyone else working on that branch.

A useful mental model: fetch is 'download mail and put it in my inbox', pull is 'download mail AND automatically file it into my folders (merge/rebase)', and push is 'mail my outgoing letters'. Fetch alone never rearranges anything you already have.

  • git fetch downloads remote changes into remote-tracking branches without touching your working branches.
  • git pull = fetch + merge (or rebase, with --rebase or pull.rebase = true) into the current branch.
  • git push uploads local commits to update a remote branch, and is rejected if it isn't a fast-forward.
  • -u/--set-upstream on the first push links a local branch to its remote counterpart for future plain push/pull.
  • --force-with-lease is a safer alternative to --force, refusing to overwrite unseen remote commits.
  • Preferring explicit fetch + review + merge/rebase over blind pull gives you a chance to inspect incoming changes first.

Practice what you learned

Was this page helpful?

Topics covered

#Bash#GitVersionControlStudyNotes#SoftwareEngineering#PushPullAndFetch#Push#Pull#Fetch#Look#StudyNotes#SkillVeris