Push to GitHub
Push to GitHub: Sharing Local Commits with Your Team
git push uploads your committed snapshots from your local repository to a remote repository such as GitHub. Only committed changes are pushed; unstaged edits and uncommitted staged files stay strictly on your machine.
Pushing for the First Time (-u)
The first time you push a branch to origin, include the -u (--set-upstream) flag so Git links your local branch to its remote counterpart:
git push -u origin main
Once upstream tracking is saved, future pushes and pulls on that branch only require typing git push or git pull with no extra arguments.
Fixing Rejected Non-Fast-Forward Pushes
If a teammate pushed commits to main after your last pull, GitHub rejects your push with [rejected] main -> main (fetch first) to protect their work from being overwritten. Pull their updates first, resolve any conflicts, and push again:
git pull --rebase origin main
git push
Why You Should Avoid --force on Shared Branches
Running git push --force overwrites remote history with your local commits and can erase teammates' pushed work. On personal feature branches after amending or rebasing, prefer --force-with-lease, which aborts safely if anyone else pushed new commits you have not fetched yet:
git push --force-with-lease origin feature/header-nav
Common Pushing Mistakes
Pushing without running git status first often happens when developers edit index.html, forget to commit, run git push, and see Everything up-to-date even though their browser shows no new changes. Always commit before pushing.
Another mistake is pushing large secret files such as .env or compiled build directories because .gitignore was missing.
Frequently Asked Questions
Does git push upload all of my local branches?
No. By default, git push uploads only your current active branch (or the branch named explicitly). To push all local branches at once, pass --all, though pushing one branch at a time is safer.
How do I verify which remote branch my local branch tracks?
Run git branch -vv to display each local branch alongside its linked origin/* upstream branch and ahead/behind commit counts.
Can I undo a commit I already pushed to main?
Yes, without rewriting history. Run git revert <commit-hash> to create a new inverse commit, then run git push normally.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With