git push
Description:
Update remote refs along with associated objects. Uploads your local commits to the remote repository.
Syntax:
bash
git push
git push <remote> <branch>
git push -u <remote> <branch>
git push --all
git push --tags
git push --force
git push --delete <remote> <branch>Examples:
bash
# Push to default remote/branch
git push
# Push to specific remote and branch
git push origin feature/new-feature
# Push and set upstream (first time)
git push -u origin main
# Push all branches
git push --all
# Push all tags
git push --tags
# Force push (dangerous!)
git push --force
# Delete remote branch
git push origin --delete old-featureDetailed Explanation:
- Upload commits - Sends your local commits to the remote
- Requires permission - You must have write access to the remote repository
- Fast-forward - Remote must be able to fast-forward (or use
--force) - Tags - By default, tags are not pushed (use
--tagsor--follow-tags)
First Push:
bash
# Set upstream tracking for new branches
git push -u origin feature/login
# Now you can use just: git pushForce Push (Dangerous):
bash
# Overwrites remote history - use with caution!
git push --force
# Safer alternative (fails if remote has new commits)
git push --force-with-leaseCommon Errors:
error: failed to push some refs→ Solution: Pull first to integrate remote changeserror: src refspec main does not match any→ Solution: Branch doesn't exist or has no commitsfatal: The current branch has no upstream branch→ Solution: Usegit push -u origin branch-namePermission denied→ Solution: Check authentication or repository permissions
Pro Tips:
- Use
git push -uonce, then justgit pushfor subsequent pushes git push --dry-runshows what would be pushed without doing it- Use
git push --force-with-leaseinstead of--forcefor safer rewrites - Warning: Never force-push to shared branches (main, develop)
- Push tags separately:
git push origin v1.0.0orgit push --tags
Related Commands:
git fetch– Download from remotegit pull– Fetch and mergegit remote– Manage remotes