Pushing and pulling

Topic 3 of 4 in Working with remotes7 min readBeginnerFree course

Overview

Fetching brings commits in. Pushing sends your commits out, and pulling fetches and integrates in one step. These three commands are how a team shares work through GitHub. This lesson covers the everyday flow and the two situations that confuse everyone at first: rejected pushes and force-pushing.

Pushing

Terminal
git push origin main      # send main to the remote origin
git push                  # if the current branch has an upstream, push to it
Text
To github.com:asha/shop.git
   a1b2c3d..e4f5a6b  main -> main

Pushing uploads your new commits and moves the branch on the remote to match yours. It only sends the branch you name (or the current one) — your other branches stay private until you push them.

Pushing a new branch

The first time you push a new branch, it has no upstream yet:

Terminal
git push -u origin feature/menu

-u (--set-upstream) links your local branch to origin/feature/menu, so from now on plain git push and git pull know where to go. With push.autoSetupRemote set (from Installing and configuring Git), plain git push does this automatically.

Deleting a remote branch

Terminal
git push origin --delete feature/menu

GitHub can also delete branches automatically when their pull requests are merged.

Rejected pushes

If someone else pushed to the branch since you last fetched, the remote has commits you don’t. Pushing would throw them away, so Git refuses:

Text
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:asha/shop.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. …

This is normal and safe. Fetch the other person’s work, integrate it, then push:

Terminal
git pull --rebase      # or: git pull (merge)
git push

Ahead, behind and diverged. After a1 and 7d, the remote’s main (origin/main) has two new commits, b2 and b3, while your main has one new commit, e4. git status says: Your branch and ‘origin/main’ have diverged, and have 1 and 2 different commits each, respectively. Options: git pull –rebase replays e4 on top of b3 for a linear history; git pull –no-rebase merges, creating a merge commit; git pull –ff-only refuses unless it can fast-forward.

Pulling

git pull is git fetch followed by integrating the remote-tracking branch into your current branch. If you have no local commits, that’s a simple fast-forward. If you and the remote both have new commits, the branches have diverged, and there are two ways to combine them:

Terminal
git pull --rebase      # replay your local commits on top of the remote ones (linear history)
git pull --no-rebase   # merge: creates "Merge branch 'main' of github.com:…"
git pull --ff-only     # do nothing unless it's a fast-forward

If you haven’t chosen, modern Git stops with:

Text
hint: You have divergent branches and need to specify how to reconcile them.
…
fatal: Need to specify how to reconcile divergent branches.

Choose once and save it:

Terminal
git config --global pull.rebase true     # recommended for most people

With pull.rebase true, your unpushed commits are always replayed on top of what others pushed, so main stays a straight line, without the merge commits that git pull would otherwise sprinkle through history. (Rebasing local, unpushed commits is safe — they’re only yours.)

Two related settings: rebase.autoStash true stashes and restores uncommitted changes around a rebase automatically, and the per-command git pull --rebase --autostash does the same once.

Force-pushing — carefully

After you rewrite history that’s already pushed — amending, rebasing or squashing your own feature branch — a normal push is rejected, because the remote’s commits aren’t in your new history:

Text
 ! [rejected]        feature/menu -> feature/menu (non-fast-forward)

You need to tell the remote to replace its version of the branch with yours. That’s a force-push, and there are two kinds:

Terminal
git push --force              # dangerous: overwrite whatever is there
git push --force-with-lease   # safe: overwrite only if the remote is where I last saw it

--force-with-lease checks that the remote branch still points to the commit your origin/feature/menu says it does. If someone else pushed in the meantime, the push is refused instead of silently deleting their work:

Text
 ! [rejected]        feature/menu -> feature/menu (stale info)

Then fetch, look at their commits, and integrate them before trying again.

Rules for force-pushing:

  • Only on your own branches — typically your feature branch before or during review.
  • Never on main or other shared branches. Protect them on GitHub so it’s impossible (Protecting branches).
  • Always --force-with-lease. Make an alias: git config --global alias.pushf "push --force-with-lease".

A typical day

Terminal
git switch main
git pull                              # get the latest main
git switch -c fix/menu-prices         # new branch for the task
# …edit, add, commit, repeat…
git fetch && git rebase origin/main   # stay up to date with main (optional)
git push                              # publish the branch (sets upstream automatically)
# open a pull request on GitHub (next chapter)

Two rules that prevent most disasters: pull before you push, and never force-push a branch someone else uses.

Exercise

Handle a rejected push

Asha and Ben both clone the same repository. Ben pushes a commit first. Asha commits too and tries to push — it’s rejected. Help Asha: pull with rebase so her commit goes on top of Ben’s, then push. Next, Asha pushes a new branch feature/menu with upstream tracking, amends its commit and has to update it on the server — do it with --force-with-lease. Finish with linear history on main and both developers in sync after Ben pulls.

Terminal
git init -q --bare -b main server.git
git clone -q server.git setup 2>/dev/null && cd setup
echo "cafe" > app.txt && git add . && git commit -qm "Start cafe" && git push -qu origin main && cd ..
git clone -q server.git asha && git clone -q server.git ben

# Ben pushes first
(cd ben && echo "tea" > tea.txt && git add . && git commit -qm "Add tea" && git push -q)

# Asha commits and tries to push
cd asha
echo "coffee" > coffee.txt && git add . && git commit -qm "Add coffee"
git push || echo "rejected!"

# 1. Pull with rebase, then push

# 2. New branch with upstream, then amend and force-push safely

# 3. Ben catches up

Summary

  • git push uploads your branch’s new commits; -u (or push.autoSetupRemote) links it to the remote branch.
  • A push is rejected if the remote has commits you don’t — fetch and integrate them first.
  • git pull = git fetch + merge or rebase; set pull.rebase so you aren’t asked each time.
  • Only force-push branches you own, and always with --force-with-lease, never plain --force.
  • Delete a remote branch with git push origin --delete <branch>.