Your repository on GitHub · lesson 6 of 9
Commands and ideas in this lesson
Agit fetchbring in what's new, change nothing of yours
Bgit pullfetch, then merge
Cgit pushsend your commits
Dgit push -upush, and remember where

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

  1. 67e435d HEAD →mainorigin/main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe
1 of 3 Joaquin pushes a commit from his computer.

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

Git sandbox
  1. Add a file halo-halo.md, commit it, and push
  2. Joaquin has just pushed. Fetch, and see his commit with git log main..origin/main
  3. Bring it into your main
  4. Make a branch desserts with 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

  1. 67e435d HEAD →mainorigin/main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe

Spot the trap: pushing without pulling

Spot the trapAna

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

Which command is always safe to run, because it never changes your branches or files?
What does git pull do?
What does -u add to git push -u origin desserts?

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.

All sources for Simple Git