Git Push

Updated · 10 min read
mediumGit 2.53gitgit-pushrejected-pushupstreaminterview

A shared online calendar only shows your new meetings to colleagues once your phone has synced. Until then, the meetings exist only on your phone, however many you add.

Your commits are the same. Every commit you make is saved only in the repository on your computer. git push sends them to the server, so that your teammates can fetch them, the hosting platform can show them, and automated builds can run on them. It is the counterpart of git fetch: fetch brings commits in, push sends them out.

In this lesson, you will push commits, read Git's report, and handle the most common push error, the rejected push.

The Starting Point

The usual setup: the server fee-tracker.git, and clones for Navika and Advitha. Navika's main was pushed with -u when she created the project, so it has origin/main as its upstream:

Terminal
git init --bare --quiet fee-tracker.git
mkdir navika
cd navika
git init --quiet fee-tracker
cd fee-tracker
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git remote add origin ../../fee-tracker.git
git push --quiet -u origin main
cd ../..
git clone --quiet fee-tracker.git advitha/fee-tracker
cd advitha/fee-tracker
git config user.name "Advitha Anamaneni"
git config user.email "advitha@example.com"
cd ../../navika/fee-tracker
git status
Output
On branch main
Your branch is up to date with 'origin/main'.
 
nothing to commit, working tree clean

Push Your Commits

Navika adds the late fee. After the commit, git status counts what is waiting to be sent:

Terminal
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git status | head -2
Output
On branch main
Your branch is ahead of 'origin/main' by 1 commit.

ahead of 'origin/main' by 1 commit: one commit exists only on this computer. Push it:

Terminal
git push
Output
To ../../fee-tracker.git
   c460dae..02ab869  main -> main
PartMeaning
To ../../fee-tracker.gitThe remote the commits went to
c460dae..02ab869The server's main moved from the old commit to the new one
main -> mainLocal main updated the server's main

git push with no arguments pushes the current branch to its upstream. In a terminal, you also see progress lines such as Writing objects: 100% before these lines.

If there is nothing new, Git says so:

Terminal
git push
Output
Everything up-to-date

The First Push of a Branch: -u

A new branch has no upstream yet, so a plain git push doesn't know where to send it. The first push names the remote and branch, and -u (--set-upstream) remembers them:

Terminal
git switch --quiet -c feature/receipts
echo "Receipt No. 1" > receipts.txt
git add receipts.txt
git commit --quiet -m "Add receipt numbers"
git push -u origin feature/receipts
Output
To ../../fee-tracker.git
 * [new branch]      feature/receipts -> feature/receipts
branch 'feature/receipts' set up to track 'origin/feature/receipts'.

* [new branch] means the branch was created on the server, and the last line confirms the upstream. From now on, git push and git pull on this branch need no arguments.

A Rejected Push

Navika goes back to main. Meanwhile, Advitha has pushed a commit to main too:

Terminal
cd ../../advitha/fee-tracker
git pull --quiet
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git push --quiet
cd ../../navika/fee-tracker
git switch --quiet main
echo "Run the app to see the fees." >> README.md
git commit --quiet -am "Explain how to run the app"
git push
Output
To ../../fee-tracker.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '../../fee-tracker.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

The push was rejected. The server's main has Advitha's hostel fee commit, which Navika doesn't have. If the server accepted Navika's main, Advitha's commit would no longer be part of main: her work would be lost. So the server only accepts a push that adds commits on top of what it has, a fast-forward. (fetch first) and the hints tell you what to do: get the new work first.

How to fix it: pull, which integrates Advitha's commit with Navika's, then push again:

Terminal
git pull --rebase
git push
Output
From ../../fee-tracker
   02ab869..dcfd724  main       -> origin/main
Successfully rebased and updated refs/heads/main.
To ../../fee-tracker.git
   dcfd724..989ec8b  main -> main

The rebase put Navika's commit on top of Advitha's, so the push is now a fast-forward and is accepted:

Terminal
git log --oneline --graph -3
Output
* 989ec8b (HEAD -> main, origin/main, origin/HEAD) Explain how to run the app
* dcfd724 Add the hostel fee
* 02ab869 Add the late fee

Syntax

Terminal
git push [options] [<remote> [<branch>]]
CommandIt is used to
git pushPush the current branch to its upstream
git push -u origin <branch>Push a branch for the first time and set its upstream
git push origin <branch>Push a branch to a named remote
git push --dry-runShow what would be pushed, without pushing
git push --tags / --follow-tagsAlso push tags (all of them, or only annotated tags that point at pushed commits)
git push origin --delete <branch>Delete a branch on the server
git push --force-with-leaseOverwrite the server's branch, safely (see "Force Push Safely")
Why a push is rejected and how to fix it

What Push Doesn't Send

  • Uncommitted changes. Only commits are pushed. Commit first.
  • Other branches. git push pushes the current branch only. Push other branches by name.
  • Tags. Tags stay local until you push them with --tags or --follow-tags, as the lessons on tags explain.
  • Your local settings. .git/config, hooks and the stash never leave your computer.

Common Mistakes

Thinking a commit is shared once it is made. Teammates see it only after you push. git status says ahead of 'origin/main' until then.

Answering a rejected push with --force. That overwrites your teammates' commits on the server. Pull and push again. Force pushing has its own lesson, for the few cases where it is right.

fatal: The current branch has no upstream branch. It is the first push of a new branch. Use git push -u origin <branch>, or set push.autoSetupRemote.

Pushing to main when your team works with pull requests. Many repositories protect main and reject direct pushes. Push a branch and open a pull request instead.

Pushing secrets. Once a password is pushed, assume it is public, even if you remove it later. Change it, and add the file to .gitignore.

Under the Hood

A push is a fetch in the other direction. Git asks the server which commit each branch points to, sends the commits the server is missing, and then asks the server to update its branch from the old commit to the new one. The server checks that the old commit is an ancestor of the new one. If it is, the update is a fast-forward and it is accepted. If not, it is rejected, which is exactly the rule you met.

On success, Git also moves your own origin/main to the new commit, which is why git status says up to date right after a push, without a fetch. Hosting platforms add their own checks on top, such as protected branches and required reviews, and report them with lines starting remote:.

Interview Corner

What does git push do?

It uploads local commits to a remote and updates the remote branch to point to the new commit, provided the update is a fast-forward. With no arguments, it pushes the current branch to its upstream.

Why was my push rejected, and how do you fix it?

The remote branch has commits your local branch doesn't have, so updating it would discard them. Run git pull (merge or rebase) to integrate those commits, resolve any conflicts, and push again. Don't force push over others' work.

What does git push -u origin <branch> do?

It pushes the branch and sets origin/<branch> as its upstream, so later git push and git pull on that branch need no arguments, and git status reports ahead/behind counts.

Does git push push tags?

No. Push them with git push origin <tag>, git push --tags for all, or git push --follow-tags for annotated tags reachable from the pushed commits.

Practice

Challenge: push twice. In Navika's clone, add Exam fee: $800 to fees.txt, commit, and run git push two times in a row. Then print the first two lines of git status.

What does Git print? Before you open the solution, decide what the second git push says, and what git status reports.

Show solution
Terminal
echo 'Exam fee: $800' >> fees.txt
git commit --quiet -am "Add the exam fee"
git push
git push
Output
To ../../fee-tracker.git
   989ec8b..68fd550  main -> main
Everything up-to-date

The first push sends the commit. The second has nothing to send: Everything up-to-date. Now the status:

Terminal
git status | head -2
Output
On branch main
Your branch is up to date with 'origin/main'.

A successful push also moves your own origin/main, so git status reports up to date with 'origin/main' without a fetch.

Checkpoint. With two clones of one repository, commit and push from the first. Commit in the second and push: read the rejection. Fix it once with git pull --rebase and once, another time, with git pull --no-rebase, and compare the histories.

Quick check: Git push

4 questions
1What does git push send to the server?
2Why would a push be rejected?
3What should you do after a rejected push?
4What does -u in git push -u origin feature do?

Key Takeaways

Quick reference

CommandIt is used to
git pushPush the current branch to its upstream
git push -u origin <branch>First push of a branch
git push origin <branch>Push a named branch
git push --dry-runPreview a push
git push --follow-tagsPush commits and their annotated tags
git pull --rebase then git pushFix a rejected push
git config --global push.autoSetupRemote trueNever need -u again

Was this tutorial helpful?

Lesson 38 of 106