Pushing and pulling
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
git push origin main # send main to the remote origin
git push # if the current branch has an upstream, push to itTo github.com:asha/shop.git
a1b2c3d..e4f5a6b main -> mainPushing 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:
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
git push origin --delete feature/menuGitHub 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:
! [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:
git pull --rebase # or: git pull (merge)
git pushPulling
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:
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-forwardIf you haven’t chosen, modern Git stops with:
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:
git config --global pull.rebase true # recommended for most peopleWith 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:
! [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:
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:
! [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
mainor 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
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.
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.
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 upgit 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!" # ! [rejected] main -> main (fetch first)
# 1. Pull with rebase, then push
git pull --rebase # Asha's commit is replayed on top of "Add tea"
git push
# 2. New branch with upstream, then amend and force-push safely
git switch -c feature/menu
echo "menu v1" > menu.txt && git add . && git commit -qm "Add menu"
git push -u origin feature/menu
echo "menu v2" > menu.txt && git commit -qa --amend --no-edit
git push --force-with-lease # only overwrites if the server still has what we last saw
cd ..
# 3. Ben catches up
cd ben && git pull --ff-only && git log --oneline
# Add coffee / Add tea / Start cafe — a straight lineSummary
git pushuploads 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; setpull.rebaseso 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>.