git push Command Guide
Update remote refs along with associated objects
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
git pushPush to tracked remotegit push origin mainPush to origin/maingit push -u origin mainPush and set upstreamForce
--forceForce push (dangerous)--force-with-leaseSafer force push--force-if-includesForce if ref includedTags
--tagsPush all tagsorigin <tag>Push specific tag--delete origin <tag>Delete remote tagDownloadable Image Preview
Basic Usage
The git push command uploads local commits to a remote repository. It's how you share your work with others.
# Push to the tracked remote branch
git push
# Push to specific remote and branch
git push origin main
# Push current branch to remote
git push origin HEADSetting Upstream
Use -u to set the upstream branch for easier future pushes:
# First push of a new branch (sets upstream)
git push -u origin feature-branch
# After setting upstream, just use:
git push
# Check what upstream is set
git branch -vvCommon Options
| Option | Description |
|---|---|
| -u, --set-upstream | Set upstream for the current branch |
| --all | Push all branches |
| --tags | Push all tags |
| --force | Force push (overwrites remote) |
| --force-with-lease | Safer force push |
| --delete | Delete remote branch or tag |
| --dry-run | Show what would be pushed |
Force Pushing
Force push rewrites remote history. Use with extreme caution:
# Force push (dangerous - overwrites remote history)
git push --force
# Safer force push (fails if remote has new commits)
git push --force-with-lease
# Force push specific branch
git push --force origin feature-branchPushing Tags
# Push a specific tag
git push origin v1.0.0
# Push all tags
git push --tags
# Push commits and tags together
git push --follow-tags
# Delete a remote tag
git push origin --delete v1.0.0Practical Examples
# Standard workflow
git add .
git commit -m "Add feature"
git push
# Push new branch and set upstream
git checkout -b feature-x
git push -u origin feature-x
# Delete remote branch
git push origin --delete old-feature
# Preview what would be pushed
git push --dry-run
# Push to different remote branch name
git push origin local-branch:remote-branch
# Push after rebase (use with care)
git push --force-with-leaseFrequently Asked Questions
What does "git push -u origin main" do?
It pushes your main branch to the origin remote and sets up tracking. After this, you can simply run "git push" without specifying the remote and branch.
What is the difference between --force and --force-with-lease?
--force overwrites remote history unconditionally. --force-with-lease only overwrites if no one else has pushed since your last fetch, preventing accidental overwrites of others' work.
Why does my push get rejected?
Usually because the remote has commits you don't have locally. Pull first (git pull), resolve any conflicts, then push again. Avoid force pushing to shared branches.
How do I push tags to the remote?
Use "git push origin <tagname>" for a specific tag, or "git push --tags" to push all tags. Tags are not pushed automatically with regular pushes.
How do I delete a remote branch?
Use "git push origin --delete <branch-name>" or the shorter syntax "git push origin :<branch-name>".
Summary
git push shares your commits with the remote repository. Always pull before pushing to avoid conflicts, and avoid force pushing to shared branches.
Quick Reference
git push- Push to tracked remotegit push -u origin <branch>- Push and set upstreamgit push --tags- Push all tagsgit push --force-with-lease- Safer force push
Official Documentation
For authoritative information, refer to the official documentation: