Pushing Shares Your Commits
git push sends your branch's new commits to the remote and moves the remote
branch to your latest commit. Push succeeds only when it simply adds to the
remote branch: everything already on the remote branch must also be in yours.
That rule protects everyone's work, and it explains every rejected push.
Upstream Branches
A branch's upstream is the remote branch it pushes to and pulls from by
default. A clone's main gets origin/main as its upstream automatically. A
branch you create yourself has none, so a plain git push stops:
fatal: The current branch agenda has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin agenda
Set it on the first push. -u is the short form of --set-upstream:
git push -u origin <branch>
Git creates the branch on the remote and reports
branch '<branch>' set up to track 'origin/<branch>'. From then on, plain
git push, git pull and the ahead/behind lines in git status all use that
upstream. git branch -vv shows each branch's upstream in square brackets.
Rejected Pushes
If the remote branch has commits yours does not, a push would throw them away, so Git rejects it. There are two forms:
! [rejected] main -> main (fetch first): the remote has commits you have not even fetched, typically because a teammate pushed.! [rejected] main -> main (non-fast-forward): your fetchedorigin/mainalready shows commits that your branch does not contain.
The fix is the same: integrate, then push again. Pull with --rebase or
--no-rebase, resolve any conflict, and push. Your commits are added after
your teammate's, and nothing is lost.
Never Force a Shared Branch
git push --force makes the remote branch match yours regardless. On a shared
branch, that deletes whatever your teammates pushed and you had not
integrated. Do not use it to get past a rejected push.
There is one legitimate case: a branch only you push to, after you rewrote its commits, for example by rebasing it. Then the rejection is expected, and you can replace the remote branch with:
git push --force-with-lease
The lease makes it safer than --force: Git replaces the remote branch only if
it is still where your last fetch saw it. If someone pushed in the meantime,
the push is rejected with (stale info), and their commits survive.
Exercise: Resolve a Rejected Push Safely
Asha publishes a new branch and updates main. Ravi's push is then rejected,
and he fixes it the right way.
Starting state: Git configured as in the configuration lesson. You will rebuild the team practice set.
-
Rebuild the practice set:
cd ~/git-practice rm -rf team mkdir team cd team git init --bare -b main shared.git git clone shared.git asha cd asha git config user.name "Asha Rao" git config user.email "asha@example.com" cat > README.md <<'EOF' # Team notes We meet on Mondays. Bring your laptop. EOF git add README.md git commit -m "Add team notes" git push -u origin main cd .. git clone shared.git ravi cd ravi git config user.name "Ravi Kumar" git config user.email "ravi@example.com" cd .. -
As Asha, start an agenda on a new branch and try to push it:
cd asha git switch -c agenda cat > agenda.md <<'EOF' # Agenda 1. Welcome EOF git add agenda.md git commit -m "Start the agenda" git pushGit stops:
The current branch agenda has no upstream branch.Push again, setting the upstream:git push -u origin agenda git branch -vvGit reports
* [new branch] agenda -> agendaandbranch 'agenda' set up to track 'origin/agenda'.git branch -vvshows[origin/agenda]next toagenda. -
Add to the agenda and push with no arguments:
cat >> agenda.md <<'EOF' 2. Plans for the month EOF git commit -am "Add monthly plans" git status git pushBefore the push,
git statussays the branch is ahead oforigin/agendaby 1 commit. The plaingit pushknows where to go. -
Still as Asha, update
main:git switch main cat >> README.md <<'EOF' Room 4 is booked for us. EOF git commit -am "Add meeting room" git push -
As Ravi, add a dial-in number and push:
cd ../ravi cat > dial-in.md <<'EOF' Join by phone: 555-0142 EOF git add dial-in.md git commit -m "Add dial-in number" git pushThe push is rejected:
! [rejected] main -> main (fetch first), because the remote contains Asha's commit, which Ravi does not have. -
Integrate, then push again. Ravi's commit has not been shared, so rebasing it is safe:
git pull --rebase git push git log --oneline origin/mainThe push succeeds.
origin/mainshows "Add dial-in number" on top of "Add meeting room". As Asha, bring it in:cd ../asha git pull git log --oneline
Success: both pushes succeeded without any force option, and the shared
main contains both Asha's and Ravi's commits.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
has no upstream branch | First push of a new branch | git push -u origin <branch> |
[rejected] with (fetch first) or (non-fast-forward) | The remote branch has commits you lack | Pull, resolve any conflict, push again |
CONFLICT during the pull | Both changed the same lines | Resolve, git add, git rebase --continue, then push |
[rejected] with (stale info) | Someone pushed since your last fetch | Fetch and look before deciding anything |
Everything up-to-date | No new commits to push | Check that you committed, and on which branch |
- Keep the practice set for the next lesson.
Independent task: agenda is Asha's branch and only she pushes to it.
Rebase it onto the updated main, then try a plain git push and predict the
result. Update the remote branch the safe way and confirm it now matches your
local branch.
Check Your Understanding
Choose between integrating and --force-with-lease:
- Your push to
mainis rejected with(fetch first). Three teammates also push tomain. - You rebased your own feature branch, which nobody else uses, and its push is
rejected with
(non-fast-forward).
Answer and Reasoning
Independent task
git switch agenda
git rebase main
git push
git push --force-with-lease
The plain git push is rejected as (non-fast-forward): the rebase replaced
the two agenda commits with new ones, so the remote branch has commits the
local branch no longer contains. Because only Asha uses agenda,
git push --force-with-lease is appropriate. Git reports a forced update,
and git status shows the branch up to date with origin/agenda.
Understanding check
- Integrate. The rejection means teammates pushed commits you do not have;
pull, resolve any conflict and push again. Forcing would delete their
commits from the shared
main. git push --force-with-lease. The rejection is the expected result of rewriting your own branch. The lease still protects you if someone did push to it unexpectedly: Git would refuse with(stale info).