Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you send commits to GitHub with git push?

Git
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 29, 2026
5 min read

Use git push to send the commits you made locally to a remote repository such as GitHub. The basic form is git push origin main, which uploads the commits of your local main branch to the remote named origin. Git only sends commits the remote does not have yet, and it refuses the push if that would overwrite someone else's work.

Before you push, the repository needs a remote and you need permission to write to it. This article covers the step that publishes your commits.

Table of Contents

  • What git push does
  • Pushing your first commits to GitHub
  • Everyday push commands
  • Why a push gets rejected
  • Force pushing safely
  • Frequently Asked Questions
    • What is the difference between git push and git commit?
    • Why does git push ask for a username and password?
    • What does git push -u mean?
    • Can I push a different local branch name to a different remote branch?
  • Summary
  • Related Articles

What git push does

Every commit you create lives only in your local repository until you push it. git push compares your branch with the matching branch on the remote, finds the commits the remote is missing, uploads them, and then moves the remote branch to point at your newest commit.

The command takes two arguments: the name of the remote and the branch to send.

bash

Here origin is the default name Git gives the remote you cloned from or added, and main is the branch. You can list your remotes with git remote -v.

Pushing your first commits to GitHub

Follow these steps for a new local project.

  1. Create an empty repository on GitHub, without a README, so that the histories do not conflict.
  2. Add it as a remote and make sure your branch is named main.
  3. Push and tell Git to remember the link.
bash

The -u flag (short for --set-upstream) links your local main to origin/main. After that, a plain git push and git pull on this branch know where to go.

Typical output looks like this:

text

GitHub asks you to authenticate on the first push. Over HTTPS you use a personal access token, and over SSH you use an SSH key; both have their own articles.

Everyday push commands

Once the upstream is set, daily work is short.

bash

Other forms you will use:

CommandEffect
git pushPushes the current branch to its upstream
git push origin feature/loginPushes a named branch, creating it on the remote if needed
git push -u origin feature/loginSame, and sets the upstream for later pushes
git push --tagsPushes all local tags
git push origin v1.0.0Pushes a single tag
git push --all originPushes every local branch

Why a push gets rejected

The most common error is a non-fast-forward rejection.

text

It means someone (or you, from another machine) pushed commits that are not in your local branch. Git refuses to push because that would discard them. The fix is to bring those commits in first.

bash

git pull fetches and integrates the remote changes. If both sides edited the same lines you resolve the conflict, finish the rebase or merge, and then push again.

Force pushing safely

Sometimes you rewrite history, for example after git commit --amend or an interactive rebase on your own feature branch. Git then rejects a normal push because your history no longer extends the remote one. You can overwrite the remote branch with a force push, but use the safer variant.

bash

--force-with-lease only overwrites the remote branch if it still points where you last saw it. A plain --force overwrites whatever is there, including a teammate's new commits. Never force push to a shared branch such as main; many teams turn on branch protection on GitHub to block it.

Frequently Asked Questions

What is the difference between git push and git commit?

git commit records a snapshot in your local repository only. git push uploads those local commits to a remote so others can see them.

Why does git push ask for a username and password?

You are using an HTTPS remote URL. GitHub no longer accepts account passwords for Git operations, so enter a personal access token, or switch the remote to SSH with git remote set-url origin [email protected]:your-name/demo.git.

What does git push -u mean?

It sets the upstream tracking branch, so later git push and git pull commands work without naming the remote and branch.

Can I push a different local branch name to a different remote branch?

Yes. Use git push origin local-name:remote-name. The first part is your local branch and the second part is the branch to update on the remote.

Summary

git push publishes local commits to a remote. Use git push -u origin main the first time, then plain git push. If Git rejects the push, pull the remote changes first. When you must rewrite history on your own branch, prefer --force-with-lease over --force.

Related Articles

  • How Do You Copy a Repository with git clone?
  • What is the difference between git fetch and git pull?
  • Learn Git and GitHub: Complete Study Roadmap, Beginner to Pro
Share