Pushing Commits
git push uploads your local commits to the remote repository.
Syntax
git push -u origin <branch>git push sends commits from your local branch up to the remote. The first time you push a branch, use -u to set the upstream, so later you can just type git push.
Upstream tracking
After git push -u origin main, Git remembers that your local main tracks origin/main. Future pushes and pulls need no extra arguments.
Example
# First push of a branch sets the upstream
git push -u origin main
# After that, this is enough
git pushWhen to use it
- A developer pushes a completed feature branch to GitHub so team members can open a pull request and review it.
- A solo developer pushes to GitHub daily as an off-site backup of their local work.
- A CI pipeline triggers automatically when a developer pushes to main, running tests and deploying to staging.
More examples
Push a branch and set upstream
The -u flag sets the upstream so subsequent pushes on this branch need only git push with no extra arguments.
git push -u origin feature/payments
# Branch 'feature/payments' set up to track 'origin/feature/payments'Push all local branches at once
Pushes every local branch to the remote in one command, useful when first publishing a repo with multiple branches.
git push --all originForce-push a rewritten branch safely
--force-with-lease protects against overwriting teammates' commits that were pushed after your last fetch.
# Safer than --force: fails if remote has new commits you don't have
git push --force-with-lease origin feature/rebase-cleanup
Discussion