switch·Esc back

Push

You might say

My local changes are ready. How do I send them to GitHub so everyone else can see them?

Upload local commits to a remote repositoryPush sends commits and branch references to a remote such as GitHub. It shares committed history, not uncommitted files. The remote may reject the push if new work exists there or if branch rules require review.
Know first
Local repositoryTwo commits ahead
Upload commitspush
Remote repositoryBranch updated on GitHub
git push

Push sends local commits to a remote branch. It does not include uncommitted edits. Afterward, confirm that GitHub shows the expected commit on the expected branch.

When to use it

  • Back up committed work to the remote
  • Share a branch with teammates
  • Update a pull request
  • Publish a reviewed commit history

When NOT to use it

  • Expect uncommitted changes to be uploaded
  • Force-push a shared branch without coordination
  • Push secrets and try to delete them later
  • Ignore a rejection without checking remote changes
Anatomy
git pushoriginmain
Publishes commits from a local branch to its remote counterpart
The remote’s short name; Git names the cloned source origin by default.
The local branch being pushed, usually main or a feature branch
Variants
git push
git push
Sync local branch commits to remote
push -u
git push -u origin main
Establish a tracking relationship between local and remote branches when pushing for the first time
push --force
git push --force
Rewrites remote history; use it only when you understand who and what it will affect
Typical use cases
Publish feature branch
zsh — my-first-page
$ git push Enumerating objects: 8, done. Counting objects: 100% (8/8), done. Writing objects: 100% (8/8), 1.24 KiB | 1.24 MiB/s, done. To github.com:maya-lee/my-first-page.git a3f9c21..e7b2d48 main -> main
Update pull request
zsh — my-first-page
$ git push To github.com:maya-lee/my-first-page.git ! [rejected] main -> main (non-fast-forward) error: failed to push some refs hint: Updates were rejected because the remote contains hint: work that you do not have locally. hint: 'git pull' before pushing again. Pull and integrate the remote commits, then push again
Share a fix
my-first-page Just refreshed
📄 index.htmlFix mobile button alignment1 minute ago
📄 style.cssFix mobile button alignment1 minute ago
📄 .gitignoreInitialize project3 days ago
✓ Latest commit found on the remote branch
Sync remote backup
Working directory
📄 index.html
📄 style.css
📄 .gitignore
📄 .env 🔑
Remote repository
📄 index.html
📄 style.css
📄 .gitignore
.env is ignored ✓
Further reading