Tour of Git
Working with Remotes / Lesson 4.4

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

  1. Git sends your new commits to the remote
  2. The remote's branch pointer moves forward
  3. Your remote tracking branch (origin/main) updates

Try it!

Create commits and push them to origin:

  1. Create a new file and commit: echo "feature" > feature.txt && git add feature.txt && git commit -m "Add feature"
  2. Push to origin: git push origin main
  3. 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.

🎯

Goal

Create a new file, commit it, and push your changes to origin

Terminal
$

No commits yet. Run `git init` and create your first commit.