Skip to the workspace
Pebellslearn by building
Git Remotes  ›  Lesson

Pushing and Pulling

Pushing / Pulling Changes 12 mingit push uploads local commits to a remote branch, git pull fetches and merges remote commits, and a push is rejected when the remote has commits you do not have.

git github / Pushing and Pulling

Explore each scene at your own pace.

01 · The rule

The rule

Pushing and Pulling

Asha committed a new soup recipe on her laptop. Ravi committed his dal recipe on his. Neither can see the other's work yet, because commits live in each person's own repository until they are sent or received. Push sends; pull receives.

The rule

Push sends your commits up; pull brings theirs down and merges them in.

Pushing uploads commits from a local branch to the matching branch on a remote:

git push -u origin main    # first time: push and remember origin/main as upstream
git push                   # afterwards: the upstream is remembered

-u (short for --set-upstream) links your local main to origin/main, so later a plain git push or git pull knows where to go. A new branch needs its own first push: git push -u origin vegan-pancakes.

Push sends commits only. Uncommitted edits and unstaged files stay on your machine.

Pulling downloads new commits from the remote and merges them into your current branch:

git pull

Under the hood, git pull is two steps: git fetch (download) followed by git merge (combine). So everything from the merging lesson applies: it can fast-forward, create a merge commit, or stop with a conflict for you to resolve. Some teams configure pull to rebase instead of merge, a choice that belongs to more advanced Git work. Recent versions of Git ask you to make this choice once, and refuse to pull diverged branches until you do. To get the merging behaviour this course uses, run:

git config --global pull.rebase false

Rejected pushes. If the remote branch has commits you do not have, Git refuses your push:

! [rejected]        main -> main (fetch first)
error: failed to push some refs

This is Git protecting other people's work. Pushing would throw their commits away. The fix is to pull first, combine the histories, then push.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.