Git Push: Upload Your Commits to a Remote Repository
git push sends your local commits to a remote repository, making them available to your teammates. This guide covers standard pushes, setting upstream branches, force pushing safely, and what to do when a push is rejected.
Beginner8 min readUpdated
What is git push?
git push uploads your local commits to a remote repository branch. If the remote branch has commits your local branch doesn't have, the push will be rejected until you integrate those changes.
The short version
- 1Push an existing tracking branch:
git push - 2Push a new branch:
git push -u origin <branch> - 3If rejected:
git pull --rebase, thengit push - 4Force push your feature branch safely:
git push --force-with-lease
Why it matters
You committed your changes locally but your teammates can't see them. You run git push and get an error: 'rejected - non-fast-forward'. Now what?
git push is how local work becomes visible to the rest of the team. But pushing to a shared branch requires care: a rejected push usually means someone else pushed first, and you need to integrate their changes before yours will be accepted.
Where it usually goes wrong
- Getting a 'rejected - non-fast-forward' error and not knowing why
- Force-pushing to a shared branch and overwriting teammates' commits
- Forgetting to set the upstream branch when pushing a new branch for the first time
- Pushing to the wrong remote or branch
Git push, step by step
Step 1: Push your current branch
If your branch already has a tracking remote, a plain
git pushis enough.Terminalgit pushTip: Run
git statusfirst: it tells you how many commits ahead of origin you are.Step 2: Push a new branch and set its upstream
When you push a brand new local branch for the first time, use
-uto set the remote tracking branch. Futuregit pushandgit pullcommands will then work without arguments.Terminalgit push -u origin feature/user-authStep 3: Push to a specific remote and branch
Explicitly specify the remote and target branch name.
Terminalgit push origin feature/user-auth# Push local branch to a differently-named remote branchgit push origin feature/user-auth:feature/user-auth-v2Step 4: Handle a rejected push (non-fast-forward)
If the remote has commits you don't have locally, your push will be rejected. You need to integrate the remote changes first.
Terminal# Error:# ! [rejected] main -> main (non-fast-forward)# Integrate remote changes first:git pull --rebase origin main# Then push again:git pushCareful: Never use
git push --forceon a shared branch (main, develop) to resolve this: you'll overwrite your teammates' commits.In Gitoryx: See 'ahead/behind' status for every branch before pushingHow it works Step 5: Force push safely with --force-with-lease
If you've rewritten your own feature branch (via rebase or amend) and need to push the rewritten history, use
--force-with-leaseinstead of--force. It refuses the push if someone else has pushed to the branch since your last fetch.Terminalgit push --force-with-lease origin feature/user-authCareful: Force-pushing is only safe on branches that only you are working on. Never force-push to shared branches.
Step 6: Delete a remote branch
After a branch is merged, clean it up on the remote.
Terminalgit push origin --delete feature/user-auth
Mistakes to avoid with git push
- Force-pushing to a shared branch
git push --forceonmainordevelopoverwrites the remote history and leaves teammates with a diverged history they can't easily reconcile.- Fix: Always pull and rebase before pushing to shared branches. Use
--force-with-leaseonly on your personal feature branches. - Forgetting `-u` on first push
- Without setting the upstream, every future
git pushon that branch requires the fullgit push origin <branch>command. - Fix: Always use
git push -u origin <branch>the first time you push a new branch. - Pushing sensitive files accidentally
- Once a file containing an API key or password is pushed, it's in the history, even if you delete it in a later commit.
- Fix: Use
.gitignoreto exclude sensitive files before they're ever added. Rotate any credentials that were accidentally exposed.
Git push in Gitoryx
Gitoryx shows you whether your branch is ahead of or behind origin before you push, and warns you if a force push would overwrite remote changes.
- See 'ahead/behind' status for every branch before pushing
- One-click push with upstream tracking set automatically
- Warning dialog before any force push operation
- Delete remote branches directly from the branch manager
Free plan · no account required · macOS, Windows and Linux
Git push: frequently asked questions
- Why is my `git push` rejected?
- The most common reason is 'non-fast-forward': the remote has commits your local branch doesn't have. Run
git pull --rebaseto integrate the remote changes, then push again. - What is the difference between `git push --force` and `--force-with-lease`?
--forceoverwrites the remote branch regardless of what's there.--force-with-leasechecks that nobody else pushed to the branch after your last fetch, making it a safer option that prevents accidentally overwriting teammates' work.- How do I push a local branch to a remote for the first time?
- Use
git push -u origin <branch-name>. The-uflag sets the upstream tracking so future pushes and pulls work without arguments. - How do I delete a remote branch?
- Run
git push origin --delete <branch-name>. You can also delete it from the GitHub or GitLab web interface.
Related reading
- Git Remote: Connect Your Repo to a Remote ServerTutorial · 8 min
- Git Branch: Create, Switch, and Manage BranchesTutorial · 9 min