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.
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
$ 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
$ 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
Sync remote backup
Working directory
📄 index.html
📄 style.css
📄 .gitignore
📄 .env 🔑
📄 style.css
📄 .gitignore
📄 .env 🔑
Remote repository
📄 index.html
📄 style.css
📄 .gitignore
.env is ignored ✓
📄 style.css
📄 .gitignore
.env is ignored ✓
Further reading