Coding

How to Push to GitHub: Commands for a New or Existing Repo

To push to GitHub, commit your changes and run git push origin main, replacing main with your branch name. The first time you push a branch, use git push -u origin main so Git remembers where it goes.

Pushing sends commits from your computer to a repository on GitHub. Nothing reaches GitHub until you do it, because git commit only saves a snapshot locally. The commands below are taken from GitHub's and Git's own documentation. They assume Git is installed and that you can sign in to GitHub from the command line, which is covered further down.

How to push an existing project to a new GitHub repository

This is the usual case: you have a folder of files on your computer and no repository on GitHub yet. Open Terminal on macOS or Linux, or Git Bash on Windows, and change into the project's root folder. GitHub's documentation gives these steps.

  1. Initialize a repository with main as the default branch.
    git init -b main
    On Git 2.27.1 or earlier, GitHub says to use git init && git symbolic-ref HEAD refs/heads/main instead.
  2. Stage every file in the folder.
    git add .
  3. Commit the staged files.
    git commit -m "First commit"
  4. On GitHub, create a new repository. GitHub's instructions say not to add a README, license or gitignore file, so the new repository is empty and has no history that conflicts with yours. On the repository's Quick Setup page, copy the remote URL.
  5. Add that URL as a remote named origin.
    git remote add origin REMOTE-URL
  6. Check that the remote was set correctly. The command lists the fetch and push URLs.
    git remote -v
  7. Push the commit.
    git push -u origin main

GitHub's page lists git push -u origin main for macOS and Linux and git push origin main for Windows. If your branch has a different name, use that name instead of main. Refresh the repository page afterwards and your files appear.

Before step 2, look through the folder for passwords, API keys and other secrets. GitHub warns that pushing sensitive data to a remote repository is a security risk, and a public repository is visible to everyone.

How to push changes to a repository you already cloned

If you started with git clone, the origin remote already exists. Edit your files, then run:

git status
git add FILE-NAME
git commit -m "Describe the change"
git push

git status shows what changed and what is staged. A plain git push works here because, per the git-push documentation, the destination defaults to the upstream of the current branch, or to origin if no upstream is configured. A clone sets up tracking for the default branch, so no extra flags are needed.

The general form is git push REMOTE-NAME BRANCH-NAME, for example git push origin main.

How to push a new branch

Create the branch, commit on it, then push it by name:

git switch -c readme-edits
git add .
git commit -m "Update the README"
git push -u origin readme-edits

The -u flag is short for --set-upstream. The git-push documentation describes it as adding an upstream (tracking) reference for every branch that is pushed successfully, which argument-less git pull and other commands use. You only need it the first time a branch is pushed.

If you would rather not type it, Git has a setting for this. When push.autoSetupRemote is true, git push sets the upstream automatically the first time you push a branch:

git config --global push.autoSetupRemote true

Once the branch is on GitHub, you can open a pull request from it. The steps are in how to use GitHub as a beginner.

How to sign in so the push is accepted

GitHub's instructions list authentication as a prerequisite: you must be able to authenticate to GitHub from the command line before pushing. There are two common routes.

  • HTTPS remote (a URL starting with https://github.com/). Use GitHub CLI or Git Credential Manager so Git stores your credentials. With GitHub CLI, run gh auth login and choose HTTPS as the Git protocol.
  • SSH remote (a URL starting with [email protected]:). Add an SSH key to your account. The steps are in how to add an SSH key to GitHub.

The remote URL you added with git remote add decides which route Git uses. If you copied the HTTPS URL, SSH keys play no part, and the other way round.

Fix: the push was rejected

This is the most common failure. Git output marks a ref as rejected when, in the words of the git-push documentation, Git "did not try to send the ref at all, typically because it is not a fast-forward and you did not force the update." GitHub's documentation describes the related message "non-fast-forward updates were rejected" and says it means your local copy is behind the remote repository.

In practice, someone (or you, from another computer or from the GitHub website) added commits to the branch on GitHub that you do not have locally. Fetch them and combine them with your work, then push again:

git pull
git push

If git pull reports conflicts, Git stops and marks the conflicting files. Edit them, run git add on each, and finish with git commit before you push.

The same rejection appears right after you create a repository on GitHub with a README and then try to push a separate local history to it. The two histories have nothing in common. That is why GitHub's instructions for an existing project say to create the repository without a README, license or gitignore file.

When to use --force or --force-with-lease

Forcing a push tells GitHub to replace the remote branch with yours. The git-push documentation says --force "can cause the remote repository to lose commits; use it with care", and that it disables the other safety checks too. --force-with-lease is a narrower override: it only proceeds if the remote branch is in the state your remote-tracking branch expects, and otherwise fails.

git push --force-with-lease

Reach for it after you have rewritten your own commits (for example with a rebase) on a branch nobody else uses. Do not force-push a shared branch such as main to get around a rejection. Pull first instead.

Fix: the push was blocked because of a secret

GitHub blocks a push if a commit contains a supported secret, such as an API key or token. The error text includes a URL. GitHub's documentation gives two ways out: remove the secret from your commits, or follow the URL to see the options for allowing the push.

Removing it means more than deleting the line in a new commit, because the secret still sits in the earlier commit that you are trying to push. Remove it from the commit that contains it before you push again. If a real credential ever reached a public repository, treat it as exposed and revoke it with the service that issued it.

Fix: no remote named origin, or the wrong URL

If Git says the remote does not exist, list what is configured:

git remote -v

An empty result means no remote has been added yet, so run git remote add origin REMOTE-URL. If origin exists but its URL is wrong, compare it with the URL on your repository's page on GitHub.

Pushing to a fork's upstream

When you work on a fork, you push to your own copy, not to the original project. GitHub's documentation describes adding a second remote, usually named upstream, that points at the original repository, using git remote add upstream THEIR_REMOTE_URL. You then run git fetch upstream to get its branches, push your own work to origin, and open a pull request. For background on how forks, branches and pull requests fit together, see how GitHub works.

Frequently asked questions

What does git push -u do?
The -u flag is short for --set-upstream. For every branch that is pushed successfully, Git records an upstream (tracking) reference, which later commands such as an argument-less git pull use. After you run git push -u origin main once, a plain git push from that branch knows where to send your commits.
Why does git push say my push was rejected?
Git refuses a push that is not a fast-forward, because it would overwrite commits that exist on GitHub but not on your computer. GitHub's documentation says this means your local copy is behind the remote repository. Fetch the remote changes, combine them with yours, then push again.
Is it safe to use git push --force?
Not as a first resort. The git-push documentation warns that --force disables the safety checks and can cause the remote repository to lose commits. If you must overwrite a branch, --force-with-lease is safer, because it fails when the remote branch is not in the state your local remote-tracking branch expects.
Why does GitHub block my push because of a secret?
GitHub's push protection blocks a push if a commit contains a supported secret, such as an API key. Remove the secret from your commits and push again. The error message includes a URL that explains the options for allowing the push if the detection is wrong.
How do I push to GitHub without typing a password every time?
Use GitHub CLI or Git Credential Manager over HTTPS so Git remembers your sign-in, or use an SSH key. Either way, you authenticate once per computer and later pushes do not prompt.

Sources