Git Foundations / Work With a Remote

Push and track an upstream branch

Lesson 26 of 33. Open to read. No account needed.

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 fetched origin/main already 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.

  1. 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 ..
    
  2. 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 push
    

    Git stops: The current branch agenda has no upstream branch. Push again, setting the upstream:

    git push -u origin agenda
    git branch -vv
    

    Git reports * [new branch] agenda -> agenda and branch 'agenda' set up to track 'origin/agenda'. git branch -vv shows [origin/agenda] next to agenda.

  3. 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 push
    

    Before the push, git status says the branch is ahead of origin/agenda by 1 commit. The plain git push knows where to go.

  4. 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
    
  5. 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 push
    

    The push is rejected: ! [rejected] main -> main (fetch first), because the remote contains Asha's commit, which Ravi does not have.

  6. 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/main
    

    The push succeeds. origin/main shows "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 seeLikely causeNext step
has no upstream branchFirst push of a new branchgit push -u origin <branch>
[rejected] with (fetch first) or (non-fast-forward)The remote branch has commits you lackPull, resolve any conflict, push again
CONFLICT during the pullBoth changed the same linesResolve, git add, git rebase --continue, then push
[rejected] with (stale info)Someone pushed since your last fetchFetch and look before deciding anything
Everything up-to-dateNo new commits to pushCheck that you committed, and on which branch
  1. 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:

  1. Your push to main is rejected with (fetch first). Three teammates also push to main.
  2. 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

  1. 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.
  2. 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).

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy