Sub topic 11
Pushing and Setting an Upstream
This chapter assumes you can create, switch, and merge local branches, and that your repository has a remote named origin. It also assumes you know what a remote-tracking branch such as origin/main is. You need Git 2.23 or later for git switch.
What an upstream is
An upstream branch is the remote branch that a local branch is paired with. Git stores the pairing in your repository config. It uses the pairing in three places:
git statusreports how far your branch is ahead of or behind its upstream.git pullknows what to fetch and merge.git pushwith no arguments knows where to send commits.
A new local branch has no upstream. The first push is where you create one.
The first push
git switch -c feature/login
git commit --allow-empty -m "Start login work"
git push -u origin feature/login-u is short for --set-upstream. The command does two things:
- It sends the commits on
feature/loginto the remoteoriginand creates a branch with that name there. - It records
origin/feature/loginas the upstream of your localfeature/login.
Without -u, the push still works, but no pairing is saved. A later bare git push then fails:
fatal: The current branch feature/login has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/loginGit is telling you to repeat the first push with -u.
What the pairing looks like in config
The pairing is two lines in .git/config:
[branch "feature/login"]
remote = origin
merge = refs/heads/feature/loginremote names the remote. merge names the branch on that remote. You can read these values with git config branch.feature/login.remote.
Reading tracking information
git branch -vv shows each local branch, its tip commit, and its upstream:
git branch -vv* feature/login 3f2a1bc [origin/feature/login] Start login work
main 9d4e7aa [origin/main: behind 2] Update README
scratch 51c0e32 Try a new ideaThe bracketed part is the upstream. These annotations can follow it:
ahead Nmeans you have N commits the remote lacks. Push them.behind Nmeans the remote has N commits you lack. Integrate them.ahead 1, behind 2means the histories have diverged.gonemeans the upstream branch was deleted on the remote.- No brackets, as on
scratch, means no upstream is configured.
These counts compare your branch with your last-fetched copy of the remote branch, not with the live remote. Run git fetch first if you want current numbers.
Pushing day to day
After the upstream exists, you can drop the arguments:
git pushWhich branch a bare git push sends depends on push.default. Since Git 2.0 the default is simple. It pushes the current branch to its upstream, and only if the names match. This prevents accidentally pushing to a branch with a different name.
You can skip -u entirely by setting this once:
git config --global push.autoSetupRemote trueWith it, the first git push of a new branch creates the remote branch and sets the upstream automatically. This option needs Git 2.37 or later.
Setting or changing an upstream later
If you pushed without -u, or want to track a different branch, use:
git branch --set-upstream-to=origin/feature/login
git branch -u origin/feature/login feature/login
git branch --unset-upstreamThe first form changes the current branch. The second names the local branch explicitly. The third removes the pairing. --set-upstream-to requires that the remote-tracking branch already exists locally, so run git fetch first if needed.
Creating a local branch from a remote one
When a teammate has pushed feature/search, you can get a tracking branch with:
git fetch origin
git switch feature/searchIf no local branch has that name and exactly one remote has it, git switch creates the local branch and sets its upstream. If two remotes both have the name, add --track origin/feature/search to say which one you mean.
Pulling and the push rejection
git pull is git fetch followed by an integration step. By default that step is a merge of the upstream into your current branch. You can make the two steps explicit:
git fetch origin
git log --oneline main..origin/main
git merge --ff-only origin/mainThe second command lists commits on the remote that you lack. The third merges only if a fast-forward is possible, and otherwise stops without changing anything. The one-command form is git pull --ff-only. To make this the default behavior, run git config --global pull.ff only.
A push is rejected when the remote branch has commits your local branch lacks:
! [rejected] feature/login -> feature/login (fetch first)
error: failed to push some refs to 'origin'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally.Git refuses because accepting the push would discard those remote commits. The fix is to integrate first, then push again:
git pull --rebase
git pushgit pull --rebase replays your local commits on top of the fetched ones. Use git pull without --rebase if your team prefers merge commits. See the merge-or-rebase chapter for how to choose.
Pushing after a rebase
A rebase gives your commits new hashes, so the remote branch and your branch no longer share history in a fast-forward way. A plain push is rejected. Use the safer force option:
git push --force-with-lease--force-with-lease overwrites the remote branch only if it still points where your last fetch saw it. If a teammate pushed in the meantime, the push fails instead of erasing their work. Plain --force has no such check.
Deleting a remote branch
Deleting a local branch does not touch the remote. To remove the remote branch after merging, run:
git push origin --delete feature/loginOther clones keep their stale origin/feature/login until they prune it:
git fetch --pruneAfter pruning, any local branch that tracked the deleted one shows gone in git branch -vv. You can then delete it with git branch -d. Setting git config --global fetch.prune true makes every fetch prune automatically.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `has no upstream branch` | Branch was pushed without `-u`, or never pushed | `git push -u origin <branch>` |
| `rejected ... (fetch first)` | Remote has commits you lack | `git pull --rebase`, then push |
| `rejected ... (non-fast-forward)` | Your history was rewritten or has diverged | Rebase or merge, or `--force-with-lease` if you own the branch |
| `[origin/x: gone]` | Remote branch was deleted | Delete the local branch, or push it again with `-u` |
| `the requested upstream branch does not exist` | Remote branch not created or not fetched | Push the branch, or run `git fetch` |
Exercise
You created feature/export and ran git push origin feature/export without -u. Later git branch -vv shows no brackets for it. Give the one command that sets its upstream without pushing again, and explain what git push would do if you ran it now with push.default set to simple.
Show answer
Run git branch --set-upstream-to=origin/feature/export (or git branch -u origin/feature/export). This works because the push already created origin/feature/export locally. Before the upstream is set, a bare git push fails with "has no upstream branch". After it is set, git push sends the current branch to origin/feature/export, because simple pushes to the upstream when the names match.
Exercise
After rebasing your own feature/login onto main, git push is rejected as non-fast-forward. What command should you use, and why is it safer than git push --force?
Show answer
Use git push --force-with-lease. It overwrites the remote branch only if the remote still points to the commit your last fetch recorded. If someone else pushed in the meantime, the push fails and their commits are not lost. Plain --force overwrites the branch regardless.