Fetch, pull and push
Last updated
Three commands keep your copy and the shared one in step. Fetch brings in what others pushed, without touching your work. Pull is fetch and then a merge. Push sends your commits up. In this lesson you'll use all three, while Joaquin pushes from his computer at the same time.
By the end of this lesson you can
- Bring in others' commits with git fetch, and see them before merging
- Explain git pull as fetch followed by merge (or rebase)
- Send commits with git push, and set an upstream with -u
- Read ahead and behind in git status
Fetch, then merge
git fetch asks origin for any commits you don't have, copies them in, and moves origin/main to match. It doesn't touch your branches or your files. git fetchSafe: only adds or reads; nothing is lost
After fetching you can look before you take anything: git log main..origin/main lists commits origin has and you don't; git diff main origin/main shows what they change. Then git merge origin/main brings them into your main, exactly like merging any branch (lesson 3.3).
(Joaquin) $ echo "4. Serve with lime wedges." >> pancit.md (Joaquin) $ git commit -am "Serve the pancit with lime" (Joaquin) $ git push $ git status On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
Remote
origin: example.com/ana/kusina.git · in step, as of your last fetch
67e435dHEAD →mainorigin/main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Pull does both
git pull is git fetch followed by git merge of the branch's upstream (usually origin/main into main). If you have no new commits of your own, the merge is a fast-forward. If you do, there's a merge commit, and possibly a conflict, settled as in lesson 3.4.
Git 2.27 and later ask you to choose, once, what pull should do when both sides have new commits. Until you do, a pull that needs more than a fast-forward stops with "Need to specify how to reconcile divergent branches". Lesson 4.7 sets this up; for now, git pull --no-rebase means "merge".
Push, and upstream branches
git push sends your branch's new commits to origin and moves the hosted branch to match. It only works if that's a fast-forward on the host: your branch must already include everything the hosted branch has (lesson 4.7).
The first time you push a new branch, Git needs to know where to: git push -u origin desserts. -u (or --set-upstream) also remembers origin's desserts as the branch's upstream, so from then on plain git push and git pull know where to go. git branch -vv shows each branch's upstream.
Ahead and behind
git status compares your branch with its upstream (as of your last fetch):
- ahead of 'origin/main' by 2 commits: you have commits to push.
- behind 'origin/main' by 1 commit: there's something to pull.
- have diverged, and have 1 and 1 different commits: both. Pull (and merge), then push.
Work alongside Joaquin
- Add a file
halo-halo.md, commit it, and push - Joaquin has just pushed. Fetch, and see his commit with
git log main..origin/main - Bring it into your main
- Make a branch
dessertswith one commit, and push it with an upstream
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
Remote
origin: example.com/ana/kusina.git · in step, as of your last fetch
You are in ~/kusina. It is connected to a hosted copy, origin, and Joaquin has his own clone on his computer.
Commit graph
67e435dHEAD →mainorigin/main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Spot the trap: pushing without pulling
Ana commits and runs git push. Git prints ! [rejected] main -> main (fetch first) and error: failed to push some refs.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-fetch: updating remote-tracking branches
- Git documentation: git-pull: fetch and integrate; merge, rebase or fast-forward only
- Git documentation: git-push: pushing, -u and the note about fast-forwards
- Pro Git: Remote Branches: pushing, tracking branches and pulling