git fetch downloads new commits from a remote without changing your local branches or files, while git pull runs git fetch and then merges or rebases those commits into your current branch. A remote is another copy of the repository, usually on a server such as GitHub. The one you cloned from is called origin. See the git fetch documentation and the git pull documentation.
What problem does git fetch solve?
You're working in my-app/ on main. You've committed a login form but haven't pushed it. Meanwhile, a colleague has pushed a fix for the RSS feed to main on origin. git status doesn't mention the fix:
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
origin/main is a remote-tracking branch: your repository's record of where main was on origin the last time Git contacted it. Git moves it only when it talks to the remote. git status compares your branch with that record and never contacts the server, so it says you're ahead even though you're also behind. See Remote Branches in Pro Git.
What does git fetch change?
git fetch
From https://github.com/example/my-app
8622f26..c6aaa9e main -> origin/main
The colleague's commit c6aaa9e is now in your repository, and origin/main points to it. Your main branch and your files haven't changed, so you can fetch at any time, even with uncommitted edits. Now git status sees both sides:
On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 1 different commits each, respectively.
Before you combine anything, you can see what arrived:
git log --oneline main..origin/main
c6aaa9e Fix RSS feed
main..origin/main means the commits on origin/main that main doesn't have. git diff main...origin/main, with three dots, shows what the colleague changed since the two branches split.
git fetch moves only origin/main. The two git pull rows are alternatives, and each starts from the first row, because git pull runs the fetch itself.What does git pull add?
git pull runs git fetch, then updates your current branch from its upstream, the remote branch it tracks. Here that's origin/main. All branches on origin are fetched, but only your current branch changes.
If you had no local commits, main would simply move forward to origin/main. This is called a fast-forward:
Updating 8622f26..c6aaa9e
Fast-forward
feed.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
In the example, both sides have a new commit, so a plain git pull stops and asks you to choose:
hint: You have divergent branches and need to specify how to reconcile them.
...
fatal: Need to specify how to reconcile divergent branches.
This output is from Git 2.50.1, and the current documentation (Git 2.55) also gives fast-forward only as the default. Older versions merged by default, so older guides may describe different behavior.
There are two ways to combine the branches:
git pull --no-rebase # merge
git pull --rebase # rebase
A merge keeps both commits as they are and adds a merge commit that joins them. In git log --oneline --graph:
* ebaff9e Merge branch 'main' of https://github.com/example/my-app
|\
| * c6aaa9e Fix RSS feed
* | 3b764e5 Add login form
|/
* 8622f26 Initial commit
A rebase replays your commit on top of the fix, so the history is a single line. Your commit becomes a new commit with a new ID, a03f04c instead of 3b764e5:
* a03f04c Add login form
* c6aaa9e Fix RSS feed
* 8622f26 Initial commit
To stop choosing every time, set a default once: git config --global pull.rebase true to rebase, or false to merge. See pull.rebase.
How do they compare?
| Effect | git fetch |
git pull |
|---|---|---|
Downloads new commits and moves origin/main |
Yes | Yes, by running git fetch |
| Changes your current branch | No | Yes: fast-forward, merge, or rebase |
| Changes the files in your folder | No | Yes |
| Can stop on a conflict | No | Yes |
| With uncommitted edits | Works as usual | Stops if the incoming commits change the same files |
What should I watch out for?
git statusonly knows about your last fetch. "Your branch is up to date with 'origin/main'" can be out of date. Rungit fetchfirst when it matters, for example before you start a new branch frommain.- A pull can stop on uncommitted edits. If the incoming commits change a file you've edited, Git refuses with
error: Your local changes to the following files would be overwritten by merge. Commit or stash your edits first.git pull --autostashstashes and restores them for you, but if your edits conflict with the incoming ones, the pull still finishes successfully and leaves conflicts in your files and your edits in the stash. - Conflicts stop a pull halfway. If both sides changed the same lines, Git pauses the merge or rebase. Fix the files and continue, or go back with
git merge --abortorgit rebase --abort. - A rebase gives your commits new IDs. That's harmless for commits only you have. If you already pushed them to a branch that others use, merge instead. See The Perils of Rebasing in Pro Git.
- Fetching doesn't remove deleted branches. If a colleague deletes
search-pageonorigin, yourorigin/search-pagestays until you rungit fetch --prune.
Which should I use?
Run git fetch whenever you want to know what's on the remote. It doesn't touch your branches or files, so it's safe at any time. Use git pull to bring your current branch up to date, after setting pull.rebase once so you know whether it will merge or rebase. When you're not sure what's coming, fetch first, read git log main..origin/main, and then pull.