Git Fetch
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:
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 --onelinec460dae (HEAD -> main, origin/main, origin/HEAD) Add README and the term feeAdvitha adds the late fee and pushes it:
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git push --quiet
git log --oneline2b34165 (HEAD -> main, origin/main, origin/HEAD) Add the late fee
c460dae Add README and the term feeYour Repository Doesn't Know Yet
Back on Navika's computer:
cd ../../navika/fee-tracker
git statusOn branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree cleanYour 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
git fetchFrom ../../fee-tracker
c460dae..2b34165 main -> origin/main| Part | Meaning |
|---|---|
From ../../fee-tracker | The remote the commits came from (origin). Git leaves out the .git ending. |
c460dae..2b34165 | origin/main moved from the old commit to the new one |
main -> origin/main | The server's main was stored as Navika's origin/main |
Now git status can tell the truth:
git status
cat fees.txtOn 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,000behind '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.
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:
git log --oneline main..origin/main
git diff main origin/main2b34165 (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: $250main..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:
git merge origin/mainUpdating 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:
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 fetchFrom ../../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:
git branch -a* main
remotes/origin/HEAD -> origin/main
remotes/origin/feature/receipts
remotes/origin/mainTo 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:
cd ../../advitha/fee-tracker
git push --quiet origin --delete feature/receipts
cd ../../navika/fee-tracker
git fetch
git branch -r origin/HEAD -> origin/main
origin/feature/receipts
origin/maingit fetch printed nothing, and the old branch is still listed. --prune removes copies of branches that no longer exist on the server:
git fetch --pruneFrom ../../fee-tracker
- [deleted] (none) -> origin/feature/receiptsgit branch -r 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
git fetch [<remote>] [<branch>] [options]| Command | It is used to |
|---|---|
git fetch | Download from the current branch's remote (usually origin) |
git fetch <remote> | Download from one remote |
git fetch --all | Download from every remote |
git fetch --prune | Also remove origin/... copies of deleted server branches |
git fetch <remote> <branch> | Download only one branch |
git fetch --tags | Also 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:
git fetch --quiet
cat .git/FETCH_HEAD2b341653efa722aba9adf041814c39eff5b9228d branch 'main' of ../../fee-trackerEach 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
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/mainOn 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 feeBefore 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.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git fetch | Download new work from origin |
git fetch --all | Download from every remote |
git fetch --prune | Also remove copies of deleted branches |
git log main..origin/main | List new commits on the server's main |
git diff main origin/main | Show the incoming changes |
git merge origin/main | Bring the fetched work into main |
git branch -r | List remote-tracking branches |
git config --global fetch.prune true | Prune on every fetch |
Was this tutorial helpful?