Pushing: Upstream Branches, push.default, Force-With-Lease, Deleting and Refspecs
Publish commits safely with git push: the first push and -u, push.autoSetupRemote and push.default, why pushes are rejected, force pushing safely with --force-with-lease and --force-if-includes, deleting and renaming remote branches, pushing tags, refspecs, and dry runs.
kvi5hnuCode Sprout team member8 min readOn this page
git push sends your commits to a remote and moves the remote's branch to match yours. It's the only everyday command that changes another repository, so Git is careful: by default it only allows a push that adds commits to the remote branch, never one that would discard someone else's. This article covers normal pushes, why pushes get rejected, how to overwrite a branch safely when you must, and the less common forms.
The first push and upstreams
A new branch exists only in your repository. The first push needs to say where it goes, and -u (--set-upstream) records that as the branch's upstream, so later git push and git pull need no arguments:
git init -q --bare server.git
git clone -q server.git sam 2>/dev/null; cd sam
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git push -u origin main
git switch -q -c lake
git commit -q --allow-empty -m "Start lake page"
echo "=== plain git push on a branch with no upstream"
git push
echo "=== with -u"
git push -u origin lake
echo "=== or let Git set the upstream automatically"
git config push.autoSetupRemote true
git switch -q -c forest
git commit -q --allow-empty -m "Start forest page"
git push
git branch -vv
To /workspace/server.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
=== plain git push on a branch with no upstream
fatal: The current branch lake has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin lake
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
=== with -u
To /workspace/server.git
* [new branch] lake -> lake
branch 'lake' set up to track 'origin/lake'.
=== or let Git set the upstream automatically
To /workspace/server.git
* [new branch] forest -> forest
branch 'forest' set up to track 'origin/forest'.
* forest 3051987 [origin/forest] Start forest page
lake 0c2eed6 [origin/lake] Start lake page
main 7fb62a0 [origin/main] Start project
git push origin mainsendsmaintoorigin'smain.-uadditionally setsorigin/mainas its upstream. You only need-uonce per branch.- Without an upstream, plain
git pushrefuses and tells you the command to run. push.autoSetupRemote=true(Git 2.37) makes the first plaingit pushof a new branch behave likegit push -u origin <branch>. Many people set it globally:
git config --global push.autoSetupRemote true
git push -u origin HEAD is a handy shortcut that pushes the current branch, whatever its name.
push.default
push.default decides what plain git push pushes:
| Value | Pushes |
|---|---|
simple (default) | the current branch to its upstream, only if the upstream has the same name |
current | the current branch to a branch of the same name, creating it if needed |
upstream | the current branch to its upstream, whatever its name |
matching | every local branch that has a same-named branch on the remote. It was the default before Git 2.0, and is risky |
nothing | nothing, unless you name what to push |
simple is the safe default. current is a convenient alternative that pushes a new branch without naming it, but unlike autoSetupRemote it doesn't record an upstream, so git pull and the ahead/behind counts in git status still need -u.
Rejected pushes
If the remote branch has commits you don't have, a push would remove them from the branch. Git refuses:
git init -q --bare server.git
git clone -q server.git sam 2>/dev/null; cd sam
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"; git push -q -u origin main
cd ..; git clone -q server.git ana
cd ana; echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page" --author="Ana Diaz <ana@example.com>"; git push -q
cd ../sam
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
git push
echo "=== integrate, then push"
git pull -q --rebase
git push
To /workspace/server.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to '/workspace/server.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.
=== integrate, then push
To /workspace/server.git
b26344b..2bf53fb main -> main
! [rejected] ... (fetch first) means "the remote has work you don't". The fix is to integrate it with git pull --rebase, or git pull with a merge (article 19), and push again. Don't reach for --force to make the message go away: that would delete Ana's commit from the server.
The variant (non-fast-forward) means you have already fetched the remote's commits but your branch doesn't include them, typically after you rebased or amended commits that were pushed. If they're only yours, that's the case for a careful force push.
Force pushing safely
After rewriting commits that were already pushed, such as rebasing your pull request branch onto main or amending a commit, the remote branch has the old commits and a normal push is rejected. A force push replaces the remote branch with yours. Three levels of force:
| Option | Overwrites the remote branch... |
|---|---|
--force (-f) | always, whatever is there. It can silently delete commits others pushed |
--force-with-lease | only if the remote branch is still where your origin/<branch> says it was. If someone pushed since your last fetch, it refuses |
--force-with-lease --force-if-includes | as above, and only if your branch includes the remote-tracking branch's tip, so a background or manual fetch can't silently make the lease pass |
git init -q --bare server.git
git clone -q server.git sam 2>/dev/null; cd sam
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"; git push -q -u origin main
git switch -q -c lake; echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake"; git push -q -u origin lake
cd ..; git clone -q server.git ana
cd ana; git switch -q lake; echo "Boats" >> lake.txt; git commit -q -am "Mention boats" --author="Ana Diaz <ana@example.com>"; git push -q
cd ../sam
git commit -q --amend -m "Add lake page" # Sam rewrites the pushed commit
echo "=== --force-with-lease: Ana's push isn't in Sam's origin/lake, so it refuses"
git push --force-with-lease
echo "=== after a fetch, the lease alone would pass; --force-if-includes still refuses"
git fetch -q
git push --force-with-lease --force-if-includes
=== --force-with-lease: Ana's push isn't in Sam's origin/lake, so it refuses
To /workspace/server.git
! [rejected] lake -> lake (stale info)
error: failed to push some refs to '/workspace/server.git'
=== after a fetch, the lease alone would pass; --force-if-includes still refuses
To /workspace/server.git
! [rejected] lake -> lake (remote ref updated since checkout)
error: failed to push some refs to '/workspace/server.git'
hint: Updates were rejected because the tip of the remote-tracking branch has
hint: been updated since the last checkout. If you want to integrate the
hint: remote changes, use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
The first refusal, (stale info), protected Ana's commit. But after git fetch, origin/lake includes Ana's commit, so --force-with-lease alone would have overwritten it, even though Sam never looked at it. --force-if-includes catches that, (remote ref updated since checkout), because Sam's branch doesn't contain the fetched commit. Sam should rebase onto origin/lake to include Ana's work, then force push.
Make the safe form your habit:
git config --global push.useForceIfIncludes true # --force-with-lease then checks this too
git config --global alias.pushf "push --force-with-lease"
Even with these, force pushing a shared branch disrupts everyone who has the old commits: their next pull duplicates or conflicts with the rewritten history (article 14). Force push your own branches, such as a pull request branch nobody else commits to, and coordinate before force pushing anything shared. Many hosting services let administrators protect branches like main, forbidding force pushes and deletion entirely.
Pushing to another name, and deleting
The full form of a push argument is a refspec: <source>:<destination>.
git push origin lake:lake-v2 # push local lake to a remote branch named lake-v2
git push origin HEAD:refs/heads/wip # push the current commit to a new branch wip
git push origin a1b2c3d:main # push main up to a specific commit only
git push origin --delete lake # delete the remote branch lake
git push origin :lake # the same: push "nothing" to lake
Pushing a commit ID to main sends that commit and its ancestors, leaving your later commits unpublished. That's handy when only some commits are ready.
Renaming a pushed branch takes three steps, because the remote doesn't rename:
git branch -m old-name new-name
git push origin -u new-name
git push origin --delete old-name
Others who had the old branch keep their copies, and see origin/old-name disappear when they git fetch --prune.
Pushing tags
Tags aren't pushed with branches by default:
git push origin v1.2.0 # one tag
git push origin --tags # all tags not yet on the remote
git push --follow-tags # branches, plus annotated tags pointing at pushed commits
git push origin --delete v1.2.0 # delete a remote tag
--follow-tags sends only annotated tags on the pushed commits, which avoids publishing stray lightweight tags. Set push.followTags=true to make it the default. Article 38 covers tags.
Other useful options
| Option | Effect |
|---|---|
--dry-run (-n) | show what would be pushed, without pushing |
--all | push all local branches |
--mirror | make the remote's references exactly match yours, deleting extras. For migrations (article 18) |
--atomic | update all requested references, or none, on servers that support it |
--no-verify | skip the local pre-push hook (article 36) |
-o <option> (--push-option) | pass an option to the server. GitLab uses this to create merge requests: -o merge_request.create |
--porcelain | stable, machine-readable output for scripts |
Reading push output
To github.com:acme/trail-guide.git
7fb62a0..b26344b main -> main normal fast-forward update
* [new branch] lake -> lake branch created
+ 4e5f6a7...9c8d7b6 lake -> lake (forced update)
- [deleted] old-name branch deleted
! [rejected] main -> main (fetch first)
Two dots mean a fast-forward, three dots and + mean a forced update, and ! means rejected. Lines starting with remote: come from the server, such as a link to open a pull request, or a message from a server-side hook explaining why a push was refused (article 36).
What's current
push.autoSetupRemote(Git 2.37) removes the need for-uon first pushes.--force-if-includes(Git 2.30) andpush.useForceIfIncludesmake--force-with-leasesafe even when fetches happen in the background, as many editors and tools do.- Hosting services offer branch protection rules and rulesets that block force pushes, require reviews and status checks, and reject pushes that contain detected secrets.
- Most services reject passwords over HTTPS: pushing needs a token or an SSH key (article 21).
Common questions
What does "Updates were rejected because the remote contains work that you do not have locally" mean?
Someone pushed to the branch after your last pull. Run git pull --rebase (or git pull), resolve any conflicts, then push again.
When is force pushing OK?
On branches only you use, after rewriting them, such as a pull request branch you rebased. Use --force-with-lease, ideally with --force-if-includes. Avoid it on shared branches unless the team agrees.
I force pushed and deleted a teammate's commits. Can they be recovered? Usually. The teammate still has them locally and can push them again. Or anyone who fetched before your push has them in their reflog or remote-tracking branch history. Some hosting services also show the previous branch position in their activity log.
Why does git push say "Everything up-to-date" when I just committed?
You may be on a different branch from the one you think, or the commit is on a branch whose upstream isn't what you expect. Check git status and git branch -vv.
How do I push a new local branch to the remote?
git push -u origin <branch>, or set push.autoSetupRemote=true and use plain git push.
Summary
git push -u origin <branch>publishes a new branch and sets its upstream.push.autoSetupRemote=truemakes plaingit pushdo that.push.default=simplepushes the current branch to its same-named upstream.- A push that would discard remote commits is rejected. Integrate with
git pull --rebase, then push. - After rewriting your own pushed commits, use
git push --force-with-lease --force-if-includes, or setpush.useForceIfIncludes. Plain--forcecan delete others' work. - Refspecs push under other names:
local:remote.git push origin --delete <branch>deletes a remote branch. - Tags need
git push origin <tag>,--tagsor--follow-tags.
Practice questions
You created feature/maps locally and committed. git push fails with "has no upstream branch". What's the one-time fix, and the permanent one?
Answer
One time: git push -u origin feature/maps, which also records the upstream so future pushes need no arguments. Permanently: git config --global push.autoSetupRemote true, so the first plain git push of every new branch sets it up.
You rebased your pull request branch onto the latest main. Why is a normal push rejected, and what's the safe way to push?
Answer
The rebase replaced the branch's commits with new ones, so the remote branch has commits your branch no longer contains, and a normal push would discard them. If nobody else commits to the branch, push with git push --force-with-lease --force-if-includes. It refuses if someone pushed to the branch in the meantime, so you don't overwrite their work unknowingly.
How do you delete the remote branch old-experiment on origin, and how does a teammate's repository find out?
Answer
git push origin --delete old-experiment (or git push origin :old-experiment). Teammates' origin/old-experiment stays until they run git fetch --prune, or have fetch.prune=true. Their own local branches aren't affected.
Previous: Fetching and Pulling: Remote-Tracking Branches, Merge or Rebase, and Divergent Branches
Next: Connecting to Remotes: HTTPS Tokens, Credential Helpers, SSH Keys and Proxies

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.