The Difference at a Glance
| git fetch | git pull | |
|---|---|---|
| Downloads commits | Yes | Yes |
| Changes your branch | No | Yes |
| Changes your files | No | Yes |
| Can cause conflicts | No | Yes |
What git fetch Does
git fetch origin
Fetch updates the remote-tracking branches, such as origin/main, which are your clone's copy of the remote's state. Your own main stays where it was. That makes fetch safe to run at any time.
See what arrived before you use it:
git log --oneline HEAD..origin/main
git diff --stat HEAD origin/main
The first command lists commits on the remote that your branch lacks; the second lists the files they change.
Fetch, Then Merge
When the changes look right, bring them in:
git merge origin/main
Together, git fetch and git merge are what git pull does in one step.
What git pull Does
git pull
Pull fetches the branch your current branch tracks and merges it. If you have no local commits, the merge is a fast-forward: your branch moves forward and no merge commit is made. If both sides have new commits, the branches have diverged, and Git must combine them.
Current Git versions do not merge diverged branches on their own when no method is configured: the pull stops with an error and suggests the options. Pick one of the options below once, globally.
pull --rebase and --ff-only
git pull --rebase: replays your local commits on top of the remote ones. History stays in one line. Set it as the default withgit config --global pull.rebase true.git pull --ff-only: updates only if a fast-forward is possible and otherwise stops without changing anything. Set it withgit config --global pull.ff only.
Rebase rewrites your local commits, so use it only for commits you have not pushed yet.
Handling Conflicts
If the same lines changed on both sides, Git stops and marks the files. Then:
- Run
git statusto see the conflicted files. - Edit each file, choose the right lines, and remove the conflict markers.
- Run
git addon each fixed file. - Finish with
git commitafter a merge, orgit rebase --continueafter a rebase.
To give up and return to where you were, run git merge --abort or git rebase --abort.
Which to Use on a Server Checkout
A deployment folder should never hold its own commits or edits. Update it in this order, as the user that owns the files:
git status
git fetch origin
git log --oneline HEAD..origin/main
git pull --ff-only
If git status shows local changes, find out who made them before you continue. Put them aside with git stash rather than discarding them. If --ff-only refuses, the checkout has commits the remote lacks; inspect them with git log --oneline origin/main..HEAD before you decide anything. Resetting the folder to the remote is covered, with its warnings, in git reset explained.
Common Problems
- Your local changes would be overwritten: commit or stash the listed files, then pull again.
- No tracking information: set the upstream with
git branch -u origin/main. - Authentication fails: check the address with
git remote -v; see how to change a Git remote URL and SSH keys with GitHub.