linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Chapters
  • How it works
  • Lessons
  • Knowledge base
  • Interview prep
home/git/kb/Remote repositories/fetch

kb/remote ── Remote repositories ── beginner

git fetch

Downloads updates from a remote but **does not touch** your local branches. Updates only refs/remotes/origin/. A safe command: after fetch you can see what arrived and decide what to do with it.

view as markdownaka: git-fetch

git fetch is half of what git pull does. Only half: download, without merge. That is its main advantage.

What physically happens

bash
git fetch origin
  1. Connects to origin (via SSH or HTTPS).
  2. Asks the remote for its list of refs and their SHAs.
  3. Downloads all commits that are not local yet, into .git/objects/.
  4. Updates refs/remotes/origin/<branch> (these are remote-tracking branches).

Local branches do not change. main, feature/x, HEAD stay where they are. Only origin/main, origin/feature/x move; these are separate refs that track the state of the remote.

bash
git log main..origin/main   # commits that arrived but are not merged yet
git diff main origin/main   # what will change on merge

This gives you a pause between "got data" and "applied it." You can inspect what came in before merging.

When to use fetch instead of pull

  • Complex merges. You want to see what arrived before merging.
  • Multiple remotes. Working via a fork (see upstream-vs-origin) usually means git fetch upstream, then decide separately what to do.
  • CI/scripts. A version-comparison script: fetch, then diff.
  • Just looking. "Who pushed what today" without touching the working tree.

Useful flags

bash
git fetch --all              # all remotes at once
git fetch --prune            # delete local refs for branches
                               # that no longer exist on the remote
git fetch --tags             # including tags
git fetch origin main        # one specific branch only
git fetch origin pull/123/head:pr-123  # GitHub PR as a local branch

--prune is especially useful in a long-lived clone: branches are created and deleted by the team, and without --prune the local list of origin/<branches> fills up with dead refs. Set it once globally: git config --global fetch.prune true.

What to do after fetch

Common patterns:

bash
# Bring local main up to date without a merge commit
git fetch origin
git switch main
git merge --ff-only origin/main
# Alternative: rebase your feature on top of the fresh main
git fetch origin
git switch feat/x
git rebase origin/main
# Just see what is new
git fetch origin
git log main..origin/main --oneline

Pitfalls

  • git fetch shows nothing in the working tree. If you expect to see new files, you still need merge or rebase.
  • Without --prune, stale branches pile up in origin/. After a year there can be dozens.
  • In CI with a shallow clone (--depth 1), fetch behaves in a limited way: history is not extended. Use --unshallow or --deepen N.

§ команды

bash
git fetch

Download updates from the default remote, without merging

bash
git fetch --all --prune

All remotes, plus remove dead refs locally

bash
git fetch origin pull/N/head:pr-N

Download a GitHub PR as a local branch

bash
git log main..origin/main

Commits that arrived but have not been merged yet

§ см. также

  • pullgit pullFetches updates from a remote repository and merges them into the current branch. Essentially `git fetch` + `git merge` (or + `git rebase` with `--rebase`). The most confusing command for beginners.
  • pushgit pushSends local commits to a remote repository and updates the branch there. If the remote received commits from someone else after your last pull, push will refuse until you synchronize.
  • remote-cmdgit remoteManages the list of URLs your repository is connected to. After `clone` there is one remote named `origin` by default. You can add a second one (`upstream` for fork flow), remove remotes, or rename them.
  • tracking-branchTracking branchThe link between a local branch and a remote one: "this branch of mine follows that remote branch." Enables `git pull`/`push` without arguments and `git status` messages like "ahead 3, behind 2."
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies