How to Push Code to GitHub

To push code to GitHub, create an empty repository on GitHub, connect your project to it with git remote add origin, and run git push -u origin main. After that first push, git push uploads your new commits.

What you'll build or solve

You'll put the git-practice project from How to Use Git on GitHub. Done looks like your files and commits showing on your repository's page at github.com.

Prerequisites

  • A free GitHub account from github.com
  • Git installed and set up with your name and email
  • A local Git repository with at least one commit

If you don't have these yet, start with How to Use Git: Install Git and Make Your First Commit.


Step-by-step instructions

Step 1: Create an empty repository on GitHub

  1. Sign in to GitHub, click + in the top-right corner, and choose New repository.
  2. Enter a name, for example git-practice.
  3. Choose Public or Private.
  4. Leave the README, .gitignore, and license options turned off.
  5. Click Create repository.

Your project already has its own files. If GitHub adds a README or license too, GitHub's repository starts with a commit your computer doesn't have, and your first push gets rejected.

GitHub then shows a page with your repository's URL and the commands for pushing an existing repository. Copy the HTTPS URL. It looks like https://github.com/<username>/git-practice.git.


Step 2: Connect your project to GitHub

In your terminal (Git Bash on Windows), go to your project folder and add the URL as a remote. Skip cd git-practice if your terminal is already in the git-practice folder, for example because you're continuing from the previous tutorial.

cd git-practice
git remote add origin https://github.com/<username>/git-practice.git

Replace <username> with your GitHub username. A remote is a copy of your repository somewhere else. origin is the usual name for the main one.

Check that it worked:

git remote -v
origin  https://github.com/<username>/git-practice.git (fetch)
origin  https://github.com/<username>/git-practice.git (push)

Step 3: Make sure your branch is called main

Check the name of your branch:

git branch
* main

The line marked * is the branch you're on. If it shows main, skip to the next step. If it shows master, rename it:

git branch -m main

Step 4: Push your commits

git push -u origin main

The first time you push, Git asks you to sign in to GitHub. See Sign in to GitHub from Git below.

After a few progress lines, the output ends like this:

To https://github.com/<username>/git-practice.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

-u links your local main branch to main on GitHub. From now on, git push and git pull know where to go without extra arguments.


Step 5: Check your repository on GitHub

Refresh your repository's page on GitHub. You should see README.md and the number of commits. Click the commit count to see the same history git log shows.


Step 6: Push new changes

From now on, every change follows the same loop: edit, stage, commit, push.

echo "Pushed from my computer." >> README.md
git add README.md
git commit -m "Add a line"
git push

The end of the output shows which commits moved:

To https://github.com/<username>/git-practice.git
   1c05a7f..ff55d47  main -> main

Your hashes will be different. Commits stay on your computer until you push them.

Pushing backs up and shares your code. It doesn't put a website online. That's deployment, a separate step.


Sign in to GitHub from Git

GitHub doesn't accept your account password for Git over HTTPS. Use one of these instead.

Git Credential Manager (Windows):

Git for Windows comes with Git Credential Manager. On your first push, a window opens and asks you to sign in. Choose to sign in with your browser and approve the request. Git remembers the login, so you don't need to sign in again.

GitHub CLI (Windows, macOS, Linux):

Install the GitHub CLI from cli.github.com. On macOS with Homebrew, run brew install gh. Then run:

gh auth login

Choose GitHub.com, choose HTTPS, answer yes when it asks to authenticate Git with your GitHub credentials, and log in with your web browser. Then run git push -u origin main again.

Personal access token:

A token is a long, random code that works like a password for Git.

  1. On GitHub, open Settings, then Developer settings, then Personal access tokens.
  2. Create a token. For a fine-grained token, under Repository access, choose your repository (or All repositories), then set Contents to Read and write. A classic token needs the repo scope.
  3. Copy the token. GitHub shows it only once.
  4. When Git asks for your username, enter your GitHub username. When it asks for your password, paste the token.

The terminal doesn't show anything while you paste. That's normal. Keep the token private, like a password.


Common errors and how to fix them

Error: remote origin already exists

error: remote origin already exists.

Why it happens:

Your project already has a remote called origin, often from an earlier attempt.

Fix:

Point origin at the right URL instead of adding it again:

git remote set-url origin https://github.com/<username>/git-practice.git

Then check it with git remote -v.


Error: src refspec main does not match any

error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/<username>/git-practice.git'

Why it happens:

Git can't find a local branch called main with commits on it. Either you haven't committed anything yet, or your branch is called master.

Fix:

Run git branch and work through these in order:

  1. If nothing is listed, you have no commits yet. Make one first:

    git add .
    git commit -m "First commit"
    
  2. Run git branch again. If it shows master, rename the branch with git branch -m main.

  3. Run git push -u origin main again.


Error: Updates were rejected because the remote contains work

 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/<username>/git-practice.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally.

Why it happens:

The repository on GitHub has a commit your computer doesn't have. On a new repository, this usually means GitHub created a README, .gitignore, or license file for you.

Fix for a brand-new repository:

If you don't need the files GitHub created, delete the repository on GitHub (Settings, then Delete this repository at the bottom), create an empty one with the same name, and run git push -u origin main again.

Fix that keeps GitHub's files:

Download GitHub's commit and put your commits on top of it:

git pull --rebase origin main

If GitHub only added files your project doesn't have, such as a license, this finishes with Successfully rebased and updated refs/heads/main.

If both sides have a README.md, Git stops with a conflict:

CONFLICT (add/add): Merge conflict in README.md

Open README.md. The part between <<<<<<< HEAD and ======= is the version already on top (GitHub's, or what you kept in an earlier round), and the part below ======= is yours. Keep the text you want, delete the marker lines, and save. Then continue:

git add README.md
git rebase --continue

Git may open your editor with the commit message. Save and close it to continue. If Git stops again with another conflict (it can, once per replayed commit), repeat the same steps (edit, git add, git rebase --continue) until you see Successfully rebased and updated refs/heads/main. To back out and return to where you were, run git rebase --abort instead.

When the rebase is done, push again. Include -u, because the rejected push didn't link your branch to GitHub:

git push -u origin main

You may find advice online to use git push --force. Don't use it to get around this error. It replaces what's on GitHub with your copy and permanently deletes the commits that are only there.


Error: The current branch main has no upstream branch

fatal: The current branch main has no upstream branch.

Why it happens:

You ran git push before your branch was linked to GitHub, or your first push failed.

Fix:

git push -u origin main

Error: Authentication failed

remote: Support for password authentication was removed on August 13, 2021.
fatal: Authentication failed for 'https://github.com/<username>/git-practice.git/'

Why it happens:

You entered your GitHub password. Git over HTTPS needs a browser sign-in or a token.

Fix:

Use one of the options in Sign in to GitHub from Git above.


Error: Repository not found

Why it happens:

The URL has a typo, the repository doesn't exist, or you're signed in to a GitHub account that can't access it.

Fix:

Run git remote -v and compare the URL with the one on your repository's GitHub page. Fix it with git remote set-url origin <correct-url>.


Quick recap

  • Create an empty repository on GitHub, without a README, .gitignore, or license.
  • Connect it with git remote add origin <url>.
  • If git branch shows master, rename it with git branch -m main.
  • Push the first time with git push -u origin main.
  • Sign in with Git Credential Manager, the GitHub CLI, or a personal access token.
  • After that: edit, git add, git commit, git push.

Next steps

Now that your project is on GitHub, learn to create a branch in Git and merge it. Branches let you try changes without touching your working code.

A GitHub profile is more convincing with real projects on it. In Mimo's Full-Stack Developer and Front-End Developer paths, you learn to code and build portfolio projects on GitHub. See all guides in the Git and GitHub tutorials.