Back
GitoryxGitoryx

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

On this page

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

  1. 1Push an existing tracking branch: git push
  2. 2Push a new branch: git push -u origin <branch>
  3. 3If rejected: git pull --rebase, then git push
  4. 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

  1. Step 1: Push your current branch

    If your branch already has a tracking remote, a plain git push is enough.

    Terminal
    git push

    Tip: Run git status first: it tells you how many commits ahead of origin you are.

  2. Step 2: Push a new branch and set its upstream

    When you push a brand new local branch for the first time, use -u to set the remote tracking branch. Future git push and git pull commands will then work without arguments.

    Terminal
    git push -u origin feature/user-auth
  3. Step 3: Push to a specific remote and branch

    Explicitly specify the remote and target branch name.

    Terminal
    git push origin feature/user-auth# Push local branch to a differently-named remote branchgit push origin feature/user-auth:feature/user-auth-v2
  4. Step 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 push

    Careful: Never use git push --force on 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
  5. 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-lease instead of --force. It refuses the push if someone else has pushed to the branch since your last fetch.

    Terminal
    git push --force-with-lease origin feature/user-auth

    Careful: Force-pushing is only safe on branches that only you are working on. Never force-push to shared branches.

  6. Step 6: Delete a remote branch

    After a branch is merged, clean it up on the remote.

    Terminal
    git push origin --delete feature/user-auth

Mistakes to avoid with git push

Force-pushing to a shared branch
git push --force on main or develop overwrites 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-lease only on your personal feature branches.
Forgetting `-u` on first push
Without setting the upstream, every future git push on that branch requires the full git 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 .gitignore to 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 --rebase to integrate the remote changes, then push again.
What is the difference between `git push --force` and `--force-with-lease`?
--force overwrites the remote branch regardless of what's there. --force-with-lease checks 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 -u flag 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.
All Git tutorials →
Gitoryx

Stop fighting Git. Start shipping.

Download the fast, native Git client that developers actually enjoy using.

v1.1.0 · macOS 12+ (Apple Silicon or Intel) · Windows 10+ · Linux x86_64