Sub topic 7
Pushing commits to a remote
This chapter assumes you can make commits and branches, know what a remote and origin are, and understand that a remote-tracking branch such as origin/main is your local record of where the remote branch was at your last fetch or push.
What a push does
git push uploads commits from a local branch to a branch on a remote. It also moves the matching remote-tracking branch on your machine. Your working tree and your other local branches are not touched.
The basic form is:
git push <remote> <branch>For example, git push origin main sends the commits on your local main to the main branch on origin. Git uploads only the objects the remote does not already have. Then it asks the remote to move its main to point at your latest commit.
The fast-forward rule
The remote accepts a push only if the move is a fast-forward. This means the remote's current commit must be an ancestor of the commit you are sending. In other words, your history must contain everything the remote already has.
If a teammate pushed first, the remote has a commit you lack. Your history and theirs have diverged, and the remote refuses the push. That protects their work from being overwritten.
Pushing a new branch
A branch that exists only locally has no counterpart on the remote. Pushing it creates one:
git switch -c feature/login
git commit -am "Add login form"
git push origin feature/loginThe remote creates feature/login, and your repository creates origin/feature/login. Adding -u (--set-upstream) also records that remote branch as the upstream of your local branch. After that, a bare git push or git pull on this branch knows where to go. The next chapter covers upstreams in detail.
Checking before you push
Compare your branch with its remote-tracking branch before you push. This shows what the push would send:
git log --oneline origin/main..main
git status -sbThe first command lists commits you have that origin/main lacks. The second prints a short status. Its first line shows ahead 2 or behind 1 relative to the upstream.
Remember that origin/main is only as fresh as your last fetch. If it is stale, run git fetch first so the comparison reflects the real remote.
Pushing to a different branch name
The full syntax is git push <remote> <local>:<remote-branch>. This pushes your local fix-typo to a remote branch called docs-fixes:
git push origin fix-typo:docs-fixesMost of the time the names match, so you can leave the colon form out.
What push does not do
- It does not push tags. Use
git push origin v1.0for one tag, orgit push --tagsfor all of them. - It does not push uncommitted changes. Only commits travel.
- It does not push every branch by default. Which branches
git pushwith no arguments sends depends on yourpush.defaultsetting, and the usual default (simple) pushes only the current branch to its upstream.
Common errors
| Message | Cause | Fix |
|---|---|---|
| `rejected ... (fetch first)` | The remote has commits you lack | Fetch, integrate, push again |
| `rejected ... (non-fast-forward)` | Your branch was rewritten or diverged | Integrate, or force with a lease if you rewrote on purpose |
| `no upstream branch` | Branch was never pushed with `-u` | `git push -u origin <branch>` |
| `Permission denied` | Credentials or access rights are wrong | Check your URL, SSH key, or token |
Exercise
You run git push origin main and Git replies ! [rejected] main -> main (fetch first). What does this tell you about the remote, and what is the next step?
Show answer
The remote main contains commits your local main does not have, so your push would not be a fast-forward. Run git fetch origin, then put your commits on top of origin/main with git rebase origin/main (or merge it), and push again.
Exercise
You created a local branch fix-typo and want it to appear on origin as docs-fixes. Which command does that?
Show answer
git push origin fix-typo:docs-fixes. The part before the colon is the local branch and the part after it is the remote branch name.