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💻 Local repositorygit push →☁️ GitHub
- Share a branch with teammates$ git push! [rejected] main -> main (non-fast-forward)hint: 'git pull' before pushing again.
- Update a pull request.gitignore
node_modules
.envUntracked files that match the rule are skipped; tracked files need separate handling. - Publish a reviewed commit historyRefresh GitHub—the latest commit is there✓ UploadedAfter pushing, verify the remote branch and latest commit
When NOT to use it
- Expect uncommitted changes to be uploaded💻 Local ●●●●☁️ EmptyIf you commit without pushing, the remote repository does not have those commits
- Force-push a shared branch without coordination$ git push --force+ a3f9c21...e7b2d48 main -> main (forced update)Existing commits on the remote branch may be rewritten
- Push secrets and try to delete them later.envPublicly visible on GitHubOnce a secret is pushed to a remote repository, treat it as exposed even if the commit is deleted
- Ignore a rejection without checking remote changesnode_modules 380MBgit push →A large number of reinstallable dependencies entered the repository; .gitignore should exclude them.
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
