Lesson 2 of 5 in Remotes & Collaboration

Pushing

Story: Work only counts once it's in HQ's archive. Send yours in.

Committing changes only your own repository. Nobody else sees your commits until you send them to the remote, and git push is how you send them.

After you commit, git status tells you that origin doesn't have the commit yet:

On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

git push sends origin the commits it's missing, and moves main on origin to your latest commit. Your origin/main moves with it, so git status says up to date again.

git push

Push sends commits and nothing else. Changes you haven't committed, staged or not, stay on your computer. Commit first, then push.

Where push sends: the upstream

With no names, git push pushes the branch you're on to its upstream, the remote branch it tracks. A clone sets that up for main: its upstream is origin/main.

You can also say exactly what to push, and where to:

git push origin main

This pushes your main to main on origin, whichever branch you're on.

Pushing a new branch

A branch you create exists only in your repository. It has no upstream yet, so a plain git push on it stops:

fatal: The current branch contact has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin contact

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

Do what it says. -u is the short form of --set-upstream:

git push -u origin contact

This creates contact on origin, sends its commits, and makes origin/contact the upstream of your contact. You need -u only once per branch. After that, git push and git pull on contact know where to go, and git status compares contact with origin/contact.

Without -u, git push origin contact still creates the branch on origin, but your contact gets no upstream. A plain git push on it keeps stopping, and git status doesn't compare it with anything.

When a push is refused

origin only accepts a push that adds to its branch. If a teammate has pushed commits you don't have, your push is rejected and nothing on origin changes. Getting their commits first is the next Lesson, Fetching and pulling. The Lesson after it, A teammate got there first, walks through a rejected push from start to finish.

New commands

  • git push
  • git push -u

Explainer: Push sends your commits to origin

project: your repo, you are hereorigin: remote host (bare), https://git.example/team/project.git99860cd Add a README99860cdd1c0363 Add the home paged1c03634607830 Style the heading4607830de1d2f7 Add the opening hoursde1d2f7d063e2f Add a contact paged063e2fmainorigin/HEADorigin/mainHEAD → contactorigin/contact99860cd Add a README99860cdd1c0363 Add the home paged1c03634607830 Style the heading4607830de1d2f7 Add the opening hoursde1d2f7d063e2f Add a contact paged063e2fmaincontact
The repository at the end of the Explainer.
Graph as text

project: your repo, you are here

  • HEAD → contact at d063e2f "Add a contact page"

Branches and tags

  • contact at d063e2f "Add a contact page", tracks origin/contact
  • main at de1d2f7 "Add the opening hours", tracks origin/main
  • origin/HEAD at de1d2f7 "Add the opening hours"
  • origin/contact at d063e2f "Add a contact page"
  • origin/main at de1d2f7 "Add the opening hours"

Commits, newest first

  1. d063e2f "Add a contact page"; parent de1d2f7; refs: HEAD → contact, origin/contact
  2. de1d2f7 "Add the opening hours"; parent 4607830; refs: main, origin/HEAD, origin/main
  3. 4607830 "Style the heading"; parent d1c0363
  4. d1c0363 "Add the home page"; parent 99860cd
  5. 99860cd "Add a README"; no parent

origin: remote host (bare), https://git.example/team/project.git

Branches and tags

  • contact at d063e2f "Add a contact page"
  • main at de1d2f7 "Add the opening hours"

Commits, newest first

  1. d063e2f "Add a contact page"; parent de1d2f7; refs: contact
  2. de1d2f7 "Add the opening hours"; parent 4607830; refs: main
  3. 4607830 "Style the heading"; parent d1c0363
  4. d1c0363 "Add the home page"; parent 99860cd
  5. 99860cd "Add a README"; no parent

Not shown: teammate.

Challenge 1 of 3

Story: A note for every agent who touches the layout. It only helps once it's in the archive.

You've added a line to README.md but haven't committed it yet. Commit it on main and push it to origin, so the rest of the team can get it.

Unlocks after Branching & Merging.

Terminal

Unlocks after Branching & Merging.

This Lesson can be played once Branching & Merging is complete. Its text and Explainer are open to everyone.