git push — Sharing Your Work
The git push command uploads your local commits to a remote repository. It's how you share your work with others.
Basic Usage
git push origin main
This pushes your local main branch to the origin remote.
Setting Upstream
The first time you push a new branch, use -u to set up tracking:
git push -u origin main
After that, you can just use git push without specifying the remote and branch.
What Happens When You Push
- Git sends your new commits to the remote
- The remote's branch pointer moves forward
- Your remote tracking branch (
origin/main) updates
Try it!
Create commits and push them to origin:
- Create a new file and commit:
echo "feature" > feature.txt && git add feature.txt && git commit -m "Add feature" - Push to origin:
git push origin main - Check the remote tracking branch has updated:
git log --oneline
Push Rejected?
If someone else pushed changes that you don't have, your push will be rejected:
! [rejected] main -> main (non-fast-forward)
The fix: git pull first to get their changes, then git push again.