TExcelTAD
Home › Git › Git Push to GitHub

Git Push to GitHub

git push uploads your local commits to a remote branch. It is the opposite of fetch: you update the server instead of your copy.

A normal push

Once upstream is set (git push -u the first time), a bare git push is enough:

bash · try it yourself
git init -q --bare ~/github/notes.git
mkdir notes && cd notes && git init -q
echo "Meeting at 10" > monday.txt && git add . && git commit -q -m "Add Monday notes"
git remote add origin ~/github/notes.git
git push -u origin main
echo "Lunch with the team" >> monday.txt && git commit -q -am "Add lunch plan"
echo "Release day" > friday.txt && git add . && git commit -q -m "Add Friday notes"
git status -sb
git push
git status -sb
To /home/learner/github/notes.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
## main...origin/main [ahead 2]
To /home/learner/github/notes.git
   3d462f9..ad7dfc5  main -> main
## main...origin/main

The line 3d462f9..ad7dfc5 main -> main means the remote main moved from the old commit to the new one, which is a fast-forward on the server.

Rejected pushes

The server only accepts a push that fast-forwards its branch. If someone else pushed first, your push is rejected so that their commits aren't thrown away:

bash · try it yourself
git init -q --bare ~/github/notes.git
mkdir notes && cd notes && git init -q
echo "Meeting at 10" > monday.txt && git add . && git commit -q -m "Add Monday notes"
git remote add origin ~/github/notes.git && git push -q -u origin main
git clone -q ~/github/notes.git ~/colleague
cd ~/colleague && echo "Bring snacks" > tuesday.txt && git add . && git commit -q -m "Add Tuesday notes" && git push -q

cd ~/notes
echo "Release day" > friday.txt && git add . && git commit -q -m "Add Friday notes"
git push
echo "exit status: $?"
To /home/learner/github/notes.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '/home/learner/github/notes.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.
exit status: 1

The fix is in the hint: integrate the remote work first, then push again:

bash · try it yourself
git init -q --bare ~/github/notes.git
mkdir notes && cd notes && git init -q
echo "Meeting at 10" > monday.txt && git add . && git commit -q -m "Add Monday notes"
git remote add origin ~/github/notes.git && git push -q -u origin main
git clone -q ~/github/notes.git ~/colleague
cd ~/colleague && echo "Bring snacks" > tuesday.txt && git add . && git commit -q -m "Add Tuesday notes" && git push -q

cd ~/notes
echo "Release day" > friday.txt && git add . && git commit -q -m "Add Friday notes"
git pull -q --rebase
git push -q
git log --oneline origin/main
11689e1 Add Friday notes
b2cae75 Add Tuesday notes
3d462f9 Add Monday notes

Force pushing (with care)

After rewriting your own commits (amend, rebase), the remote branch no longer fast-forwards and a normal push is rejected. git push --force overwrites the remote branch anyway, which deletes any commits there that you don't have.

The safer variant is --force-with-lease: it only overwrites if the remote branch is still where you last saw it. If a colleague pushed in the meantime, it refuses:

bash · try it yourself
git init -q --bare ~/github/notes.git
mkdir notes && cd notes && git init -q
echo "draft" > plan.txt && git add . && git commit -q -m "Add plan"
git remote add origin ~/github/notes.git && git push -q -u origin main
git clone -q ~/github/notes.git ~/colleague
cd ~/colleague && echo "budget" > budget.txt && git add . && git commit -q -m "Add budget" && git push -q

cd ~/notes
git commit -q --amend -m "Add project plan"
git push --force-with-lease
echo "exit status: $?"
git fetch -q
git log --oneline origin/main
To /home/learner/github/notes.git
 ! [rejected]        main -> main (stale info)
error: failed to push some refs to '/home/learner/github/notes.git'
exit status: 1
09bfff7 Add budget
c9c93c8 Add plan

The lease protected the colleague's "Add budget" commit. (Fetching updates the lease, so look at what you fetched before retrying.)

Warning: Never force-push main or any branch others build on. Most teams enable branch protection on GitHub, which blocks force pushes to main completely.

Useful push forms

Command Does
git push push the current branch to its upstream
git push -u origin feature push a new branch and set its upstream
git push origin v1.0 / --tags push one tag / all tags
git push origin --delete old-branch delete a branch on the remote
git push --force-with-lease overwrite your own rewritten branch safely
git push --dry-run show what would be pushed

Test yourself

Your push is rejected with "fetch first". Why?

  1. Your network is down
  2. The remote branch has commits you don't have, so your push isn't a fast-forward
  3. You have uncommitted changes
  4. The branch is protected

Why is --force-with-lease safer than --force?

  1. It is faster
  2. It only overwrites the remote branch if nobody else pushed since your last fetch
  3. It creates a backup branch
  4. It merges automatically