Skip to content
Laviyx Learn

Try

Git and GitHub course menu

How to Push a Project to GitHub for the First Time

Create a GitHub account and repository, connect a local project with git remote add, push it with git push -u origin main, and understand remotes, origin and upstream branches.

Beginner6 min readUpdated
On this page

So far your history exists only on your computer. GitHub keeps a copy online, which gives you a backup, a way to share code, and a public record of your work.

What is a remote?#

A remote is a named link to another copy of your repository. By convention, the main one is called origin.

Text
Your computer                     GitHub
notes (main)   -- git push -->    your-name/notes (main)
               <-- git pull --

Create a repository on GitHub#

  1. Sign up at github.com. Your username becomes part of your profile address, so choose something professional.
  2. Click + at the top right, then New repository.
  3. Enter a name such as notes.
  4. Choose Public or Private.
  5. Leave Add a README, .gitignore and license unset, because you already have a local project.
  6. Click Create repository.

GitHub then shows the address of your new, empty repository.

Connect and push#

In these commands, your-name stands for your GitHub username. Connect your local repository:

Terminal
git remote add origin https://github.com/your-name/notes.git
git remote -v
Output
origin	https://github.com/your-name/notes.git (fetch)
origin	https://github.com/your-name/notes.git (push)

Make sure your branch is called main, then push it:

Terminal
git branch -M main
git push -u origin main
Output
To https://github.com/your-name/notes.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

Refresh the page on GitHub, and your files and commits are there. The first time, Git asks you to sign in. GitHub no longer accepts your account password here, so see SSH Keys and Access Tokens if that step fails.

What -u does#

-u (short for --set-upstream) links your local main to origin/main. You only need it once per branch. After that, plain git push and git pull know where to go, and git status compares the two:

Terminal
git status
Output
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

The everyday flow#

Now the cycle has one more step:

Terminal
echo "Call the bank" >> notes.txt
git add notes.txt
git commit -q -m "Add bank reminder"
git status
Output
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

ahead of 'origin/main' by 1 commit means you have a commit that GitHub does not. Push it:

Terminal
git push
git status
Output
To https://github.com/your-name/notes.git
   3b1b8d5..528d325  main -> main
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

origin/main is called a remote-tracking branch. It is your local record of where main was on GitHub the last time you communicated with it.

Push a new branch#

Terminal
git switch -q -c add-readme
echo "# Notes" > README.md
git add .
git commit -q -m "Add README"
git push -u origin add-readme
git branch -a
Output
To https://github.com/your-name/notes.git
 * [new branch]      add-readme -> add-readme
branch 'add-readme' set up to track 'origin/add-readme'.
* add-readme
  main
  remotes/origin/add-readme
  remotes/origin/main

Managing remotes#

CommandWhat it does
git remote -vList remotes and their addresses
git remote add name urlAdd a remote
git remote set-url origin urlChange an address
git remote remove nameRemove a remote

Common mistakes#

Practice#

Practice exercise

Print the remote's name and address, and then print which remote branch your current branch tracks, using git rev-parse --abbrev-ref @{upstream}.

Show solution
Terminal
git remote -v
git rev-parse --abbrev-ref @{upstream}
Output
origin	https://github.com/your-name/notes.git (fetch)
origin	https://github.com/your-name/notes.git (push)
origin/add-readme

Key takeaway#

Next: SSH Keys and Access Tokens.

Finished this lesson?