Git Push
Last updated
git push uploads the commits on your local branch to its remote counterpart (like GitHub). Once a branch has an upstream set, a plain git push is enough; the first push of a new branch uses -u to create the remote branch and set up tracking so future pushes and pulls need no arguments.
Try these in the terminal playground - a real shell in your browser, nothing to install.
Syntax
| Command | What it does |
|---|---|
git push | Push the current branch to its upstream |
git push -u origin feature | Push a new branch and set upstream |
git push origin main | Push to a specific remote and branch |
git push --tags | Push all local tags |
git push --force-with-lease | Force push safely after a rebase |
Common cases
| Goal | Command |
|---|---|
| Push a brand-new branch | git push -u origin feature |
| Push after the upstream is set | git push |
| Push a single tag | git push origin v1.0.0 |
| Delete a remote branch | git push origin --delete feature |
Worked example
| Step | Command | Result |
|---|---|---|
| 1 | git remote add origin <url> | Connects the local repository to the remote under the name origin |
| 2 | git push -u origin main | Creates main on the remote and sets it as the upstream of your local main |
| 3 | git push | Every later push on main needs no arguments |
Git push FAQ
What does git push do?
It sends the commits your local branch has and the remote branch lacks, then moves the remote branch to match. It uploads commits only: uncommitted changes stay on your machine, so commit first. If the remote has commits you don't have, the push is rejected until you pull or rebase.
How do I push a new branch to the remote?
Run
git push -u origin <branch>. The -u (short for --set-upstream) creates the branch on the remote and links your local branch to it, so afterwards a plain git push and git pull work without arguments. The remote branch doesn't exist until this first push.Why does git push say 'no upstream branch'?
Your local branch isn't linked to a remote branch yet. Push it with
git push -u origin <branch> to create the remote branch and set the upstream. After that, git push alone knows where to send commits.How do I push tags?
Tags aren't pushed by default. Push a single tag with
git push origin <tagname>, or push all of them at once with git push --tags. This is a common gotcha - a tag you created locally won't appear on the remote until you push it explicitly.How do I force push safely?
After rewriting history (a rebase or amend), use
git push --force-with-lease rather than --force. It only overwrites the remote if no one else has pushed since your last fetch, protecting teammates' work. See the git force push page for the full explanation.What does git push -u origin main mean?
origin is the remote (see git remote), main is the branch, and -u sets that remote branch as the upstream of your local main. It is the command GitHub shows for a new repository: after it runs once, git push and git pull on main need no arguments.What is the difference between git push and git pull?
They move commits in opposite directions.
git push uploads your local commits to the remote; git pull downloads the remote's new commits and merges them into your branch. A push rejected as non-fast-forward usually means you need to pull first. See git fetch vs pull for what pull does under the hood.Can I practice this online?
Yes. Open the terminal playground to run
git push in a real shell in your browser - nothing to install. Coddy's free interactive Git course also covers working with remotes step by step.