Sub topic 5
Fetching: downloading without changing your work
This chapter assumes you can commit and switch branches, and that you know what a remote-tracking branch such as origin/main is. It also assumes a remote named origin is already configured.
What fetch does
git fetch contacts a remote, downloads commits and other objects you don't have, and updates your remote-tracking branches. A remote-tracking branch is your local record of where a branch on the remote was at the last contact.
Fetch changes only that record and the object database. It does not touch:
- your local branches, such as
main - your working tree
- the staging area
This makes fetch safe to run at any time, even with uncommitted changes.
git fetch originGit asks origin which branches it has. It downloads any commits you lack, then moves each origin/<branch> pointer to match.
A worked example
Suppose a teammate pushed two commits to main on the remote. Your local main still has the old tip. Run:
git fetch origin
git statusThe output includes a line like this:
Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded.Before the fetch, Git had no way to know about those commits. After it, origin/main points at them, and main is two commits behind. Your files are unchanged. The next chapter covers how to look at those commits before you integrate them.
Choosing what to fetch
Fetch takes several forms:
| Command | Effect |
|---|---|
| `git fetch` | Fetches from the current branch's upstream remote, or `origin` if none is set |
| `git fetch origin` | Fetches all branches from `origin` |
| `git fetch origin main` | Fetches only `main` from `origin` |
| `git fetch --all` | Fetches from every configured remote |
Fetching one branch still updates origin/main. Git does this because the remote's default fetch rule maps refs/heads/* on the remote to refs/remotes/origin/* locally. You can see that rule in your configuration:
git config --get-all remote.origin.fetchIt prints +refs/heads/*:refs/remotes/origin/*. The leading + lets the remote-tracking branch move even when the update is not a fast-forward. That matters after someone force-pushes, because your origin/main simply follows the remote.
Fetch also downloads tags that point at the commits it brings in. Nothing is merged into your branches.
FETCH_HEAD
Each fetch writes a file, .git/FETCH_HEAD, that lists what was just downloaded. You can use it as a reference, as in git log FETCH_HEAD. It is overwritten by the next fetch, so for anything you plan to keep, use the remote-tracking branch name instead.
Common errors
- Expecting fetch to update your files. If
git logshows nothing new after a fetch, you are looking atmain. Look atorigin/maininstead. - Fetching a stale view.
origin/mainis only as fresh as your last fetch. If you decide something based onorigin/mainhours later, fetch again first. - Old branches that never disappear. A plain fetch adds and updates remote-tracking branches, but it does not remove ones whose remote branch was deleted. That cleanup is a separate option, covered in a later chapter.
- Authentication prompts. Fetch needs read access to the remote. A failed prompt or "Permission denied" points to credentials, not to Git history.
Fetch compared with pull
git pull is a fetch followed by an integration step, either a merge or a rebase. Fetch is the first half of that command. Running the halves separately gives you a chance to look before your branch moves. Later chapters cover that integration step and the ways to review incoming work.
Exercise
You run git fetch origin and then git log --oneline on your checked-out main. The output shows nothing new, but a teammate pushed commits to the remote's main. Where can you see those commits, and why did git log not show them?
Show answer
They are on the remote-tracking branch origin/main, for example with git log --oneline origin/main. Fetch moves origin/main but leaves your local main unchanged, and git log without a branch name shows the current branch only.