Git Fetch

Updated · 10 min read
mediumGit 2.53gitgit-fetchoriginpruneinterview

When the post arrives, it goes into your letterbox. You can take the letters inside, read them, and decide what to do with each one. Nothing in your house changes until you decide. Your notes on the kitchen table stay exactly as they were.

git fetch is the postman. It downloads the commits your teammates pushed to the server and stores them in your repository, but it doesn't change your branches or your files. You can then look at what came in, and decide when and how to combine it with your work. Because it never touches your work, git fetch is always safe to run, and many developers run it many times a day.

In this lesson, you will fetch a teammate's work, see where it is stored, check it before merging, and keep your list of server branches tidy.

The Starting Point

A bare repository fee-tracker.git plays the server, as in the previous lessons. Navika pushed the project, and Advitha cloned it. On Advitha's computer, her own name is configured, as it would be:

Terminal
git init --bare --quiet fee-tracker.git
mkdir navika
cd navika
git init --quiet fee-tracker
cd fee-tracker
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git remote add origin ../../fee-tracker.git
git push --quiet -u origin main
cd ../..
git clone --quiet fee-tracker.git advitha/fee-tracker
cd advitha/fee-tracker
git config user.name "Advitha Anamaneni"
git config user.email "advitha@example.com"
git log --oneline
Output
c460dae (HEAD -> main, origin/main, origin/HEAD) Add README and the term fee

Advitha adds the late fee and pushes it:

Terminal
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git push --quiet
git log --oneline
Output
2b34165 (HEAD -> main, origin/main, origin/HEAD) Add the late fee
c460dae Add README and the term fee

Your Repository Doesn't Know Yet

Back on Navika's computer:

Terminal
cd ../../navika/fee-tracker
git status
Output
On branch main
Your branch is up to date with 'origin/main'.
 
nothing to commit, working tree clean

Your branch is up to date with 'origin/main' sounds reassuring, but it is out of date. Git doesn't contact the server for git status. It compares main with Navika's own copy of the server's branch, origin/main, as it was the last time she talked to the server. That copy doesn't know about Advitha's push yet.

Fetch

Terminal
git fetch
Output
From ../../fee-tracker
   c460dae..2b34165  main       -> origin/main
PartMeaning
From ../../fee-trackerThe remote the commits came from (origin). Git leaves out the .git ending.
c460dae..2b34165origin/main moved from the old commit to the new one
main -> origin/mainThe server's main was stored as Navika's origin/main

Now git status can tell the truth:

Terminal
git status
cat fees.txt
Output
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
 
nothing to commit, working tree clean
Term 1 fee: $12,000

behind 'origin/main' by 1 commit: there is new work to bring in. But the file in the folder still has no late fee. Fetch downloaded the commit into the repository and moved origin/main, nothing else. Navika's main and her working tree are exactly as before.

git fetch updates origin/main and nothing else

Look Before You Merge

This is the reason to fetch on its own: you can look at the new work first. Two-dot ranges with git log and git diff show what is new:

Terminal
git log --oneline main..origin/main
git diff main origin/main
Output
2b34165 (origin/main, origin/HEAD) Add the late fee
diff --git a/fees.txt b/fees.txt
index 2de12a7..b73e396 100644
--- a/fees.txt
+++ b/fees.txt
@@ -1 +1,2 @@
 Term 1 fee: $12,000
+Late fee: $250

main..origin/main means "commits in origin/main that are not in main": exactly what a merge would bring in. When Navika is happy with it, she merges it like any other branch:

Terminal
git merge origin/main
Output
Updating c460dae..2b34165
Fast-forward
 fees.txt | 1 +
 1 file changed, 1 insertion(+)

A fast-forward, because Navika had no new commits of her own. Fetching and then merging is exactly what git pull does in one step, as the next lesson shows.

A Teammate's New Branch

Advitha starts a new feature and pushes the branch:

Terminal
cd ../../advitha/fee-tracker
git switch --quiet -c feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
git push --quiet -u origin feature/receipts
cd ../../navika/fee-tracker
git fetch
Output
From ../../fee-tracker
 * [new branch]      feature/receipts -> origin/feature/receipts

* [new branch] means the server has a branch Navika's repository hadn't seen. It is stored as origin/feature/receipts:

Terminal
git branch -a
Output
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/feature/receipts
  remotes/origin/main

To work on it, Navika runs git switch feature/receipts, which creates a local branch from it, as you saw in "Git Clone".

Remove Branches That Are Gone: --prune

Advitha's feature is merged on the server later, and she deletes the branch there. Navika's origin/feature/receipts doesn't disappear by itself, because a plain fetch only adds and updates:

Terminal
cd ../../advitha/fee-tracker
git push --quiet origin --delete feature/receipts
cd ../../navika/fee-tracker
git fetch
git branch -r
Output
  origin/HEAD -> origin/main
  origin/feature/receipts
  origin/main

git fetch printed nothing, and the old branch is still listed. --prune removes copies of branches that no longer exist on the server:

Terminal
git fetch --prune
Output
From ../../fee-tracker
 - [deleted]         (none)     -> origin/feature/receipts
Terminal
git branch -r
Output
  origin/HEAD -> origin/main
  origin/main

- [deleted] reports the removed copy. To prune on every fetch, set it once: git config --global fetch.prune true.

Syntax

Terminal
git fetch [<remote>] [<branch>] [options]
CommandIt is used to
git fetchDownload from the current branch's remote (usually origin)
git fetch <remote>Download from one remote
git fetch --allDownload from every remote
git fetch --pruneAlso remove origin/... copies of deleted server branches
git fetch <remote> <branch>Download only one branch
git fetch --tagsAlso download every tag

Common Mistakes

Trusting "up to date" without fetching. git status compares with your last copy of the server. Fetch first.

Expecting fetch to change your files. It doesn't. Merge origin/main or use git pull to bring the work into your branch.

Committing on origin/main. origin/... branches are copies of the server, updated only by fetch. git switch origin/main puts you in detached HEAD. Switch to main and merge instead.

Collecting dozens of deleted branches. Without --prune, git branch -r keeps showing branches that were deleted long ago. Turn on fetch.prune.

Fetching from the wrong remote. With several remotes, git fetch uses the current branch's remote. Name it to be sure: git fetch upstream.

Under the Hood

A fetch runs in two steps. First, Git asks the server which branches exist and which commits they point to, works out which commits your repository is missing, and downloads only those. Second, it updates the remote-tracking branches, following the remote's refspec, +refs/heads/*:refs/remotes/origin/*: every server branch refs/heads/X is stored as your refs/remotes/origin/X. The + allows a remote-tracking branch to be moved even when it isn't a fast-forward, which happens after someone force-pushes.

Git also writes what it fetched to .git/FETCH_HEAD:

Terminal
git fetch --quiet
cat .git/FETCH_HEAD
Output
2b341653efa722aba9adf041814c39eff5b9228d		branch 'main' of ../../fee-tracker

Each line is a fetched branch with its commit. When several branches are fetched, the ones marked not-for-merge are only stored, and the one without that mark (here the only line, main) is the branch git pull would merge into your current branch. That file is how git pull passes the result of its fetch to its merge.

Interview Corner

What does git fetch do?

It downloads new commits and branches from a remote and updates the remote-tracking branches, such as origin/main. It doesn't change local branches, the staging area or the working tree, so it is always safe.

What is the difference between git fetch and git pull?

git fetch only downloads and updates origin/.... git pull runs git fetch and then integrates the current branch's upstream into it, with a merge or a rebase. Fetching first lets you inspect incoming changes before integrating them.

Why does git status say "up to date" when a teammate has pushed?

git status compares your branch with your local copy of the remote branch (origin/main), which only changes when you fetch. It doesn't contact the server. Run git fetch first.

How do you see what a fetch brought in before merging it?

git log main..origin/main lists the new commits, and git diff main origin/main (or main...origin/main) shows the changes.

What does git fetch --prune do?

Besides fetching, it deletes remote-tracking branches whose branches no longer exist on the remote. fetch.prune=true makes it the default.

Practice

Challenge: behind or not? Advitha pushes one more commit to main, adding Hostel fee: $9,000. On Navika's computer, print git status before and after git fetch, and the short log of main..origin/main.

What does Git print? Before you open the solution, decide what the first git status says, and whether fees.txt changes after the fetch.

Show solution
Terminal
cd ../../advitha/fee-tracker
git switch --quiet main
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git push --quiet
cd ../../navika/fee-tracker
git status | head -2
git fetch --quiet
git status | head -2
git log --oneline main..origin/main
Output
On branch main
Your branch is up to date with 'origin/main'.
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
48fb59c (origin/main, origin/HEAD) Add the hostel fee

Before the fetch, git status says up to date, because Navika's origin/main still has the old position. After it, she is behind 'origin/main' by 1 commit, and the log shows the hostel fee commit. fees.txt in her folder is unchanged until she merges or pulls.

Checkpoint. In a repository you share with someone, or two clones of one repository, push a commit from one clone. In the other, compare git status before and after git fetch, then inspect the new commit with git log -p main..origin/main before you merge it.

Quick check: Git fetch

4 questions
1What does git fetch change?
2Why can git status say "up to date" when it isn't?
3Which command lists the commits a fetch brought in for main?
4A branch was deleted on the server, but git branch -r still shows it. What do you run?

Key Takeaways

Quick reference

CommandIt is used to
git fetchDownload new work from origin
git fetch --allDownload from every remote
git fetch --pruneAlso remove copies of deleted branches
git log main..origin/mainList new commits on the server's main
git diff main origin/mainShow the incoming changes
git merge origin/mainBring the fetched work into main
git branch -rList remote-tracking branches
git config --global fetch.prune truePrune on every fetch

Was this tutorial helpful?

Lesson 36 of 106