Git Push
git push uploads your local commits to a remote repository, making your changes available to others. It's the complement to git pull and essential for collaboration.
Basic Push
Push to Remote
bash
# Push current branch to remote
git push
# Push to specific remote and branch
git push origin mainFirst Push (Set Upstream)
bash
# Push and set upstream tracking
git push -u origin branch-name
# Now future pushes just need
git pushExample:
bash
$ git push -u origin feature-login
Counting objects: 5, done.
Writing objects: 100% (5/5), 450 bytes | 450.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/user/repo.git
* [new branch] feature-login -> feature-login
Branch 'feature-login' set up to track remote branch 'feature-login' from 'origin'.Push Options
Push All Branches
bash
# Push all branches
git push --all
# Push all branches to specific remote
git push origin --allPush Tags
bash
# Push specific tag
git push origin v1.0.0
# Push all tags
git push --tags
# Push tags to specific remote
git push origin --tagsForce Push
bash
# Force push (DANGER!)
git push --force
# Safer force push
git push --force-with-lease⚠️ Warning: Only force push on branches you own!
Delete Remote Branch
bash
# Delete remote branch
git push origin --delete branch-name
# Alternative syntax
git push origin :branch-nameDry Run
bash
# See what would be pushed without pushing
git push --dry-runPush Strategies
Simple Push (Default)
Push current branch to its upstream:
bash
git pushPush to Different Branch Name
bash
# Push local 'feature' to remote 'feature-v2'
git push origin feature:feature-v2Push Multiple Branches
bash
# Push multiple branches at once
git push origin main develop featureCommon Scenarios
Scenario 1: First Time Push
bash
# Create branch and commit
git checkout -b new-feature
git commit -m "Add feature"
# Push and set upstream
git push -u origin new-featureScenario 2: Regular Push
bash
# Make changes
git add .
git commit -m "Update feature"
# Push
git pushScenario 3: Push After Rebase
bash
# Rebase your branch
git rebase main
# Force push (your branch only!)
git push --force-with-leaseScenario 4: Push to Fork
bash
# Push to your fork
git push origin main
# Push to upstream (if you have write access)
git push upstream mainTroubleshooting
Push Rejected (Non-Fast-Forward)
bash
$ git push
To https://github.com/user/repo.git
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/user/repo.git'Solution:
bash
# Pull first
git pull origin main
# Or pull with rebase
git pull --rebase origin main
# Then push
git pushNo Upstream Branch
bash
$ git push
fatal: The current branch feature has no upstream branch.Solution:
bash
git push -u origin featurePermission Denied
bash
$ git push
ERROR: Permission to user/repo.git deniedSolution:
- Check repository access
- Verify SSH keys
- Check remote URL
- Ensure you're authenticated
Best Practices
- Always pull before pushing
- Use
--force-with-leaseinstead of--force - Push regularly to backup your work
- Don't force push to shared branches
- Push feature branches to create backups
See Also
- Git Fetch & Pull - Fetching and pulling
- Git Remote - Managing remotes
- Git Branch - Branch management