ProgramGuru

Connect an existing local repository to GitHub

Connect an existing local repository to GitHub

You already have local commits in notes-app. Now you want GitHub to host a shared copy without losing the history you built locally.

What you’ll learn

  • Add and verify a GitHub remote.
  • Push an existing branch.
  • Explain upstream tracking.

Publish existing history

  1. Local repo

    You already have commits.

    main
  2. Remote URL

    Add GitHub as origin.

    git remote add
  3. Push

    Send main and configure tracking.

    origin/main
origin/main

origin is a conventional remote name, not a special server

After creating an empty repository on GitHub, add its URL as a remote—commonly named origin. The local repository then knows where to fetch from and push to.

git push -u origin main pushes the local main branch and sets its upstream tracking relationship. Later, plain git push and git pull can often use that configured relationship.

Publish your local main branch

Run this in a disposable practice repository unless the lesson explicitly says otherwise.

git remote add origin https://github.com/USERNAME/notes-app.git
git remote -v
git push -u origin main

Replace USERNAME with the actual owner. Authentication may use GitHub CLI, a credential helper/personal access token over HTTPS, or SSH depending on your setup.

Practice: Push an existing project

Your task: After setting an upstream, run git branch -vv and identify the local branch and its tracked remote branch.

Common pitfall to avoid

Copy the remote URL from the intended repository and inspect it with git remote -v. Accidentally pushing to the wrong owner or repository is a preventable configuration mistake.

Check your understanding

What does -u do on the first git push in this workflow?
Choose one answer. You can try again.

Answer and explanation

It sets the upstream tracking relationship for the branch.

Correct.

Push an existing project: what to remember

Connect an existing local repository to GitHub is complete when you can explain the state being changed before you run the command, not merely repeat the syntax.

Read further: official supporting documentation.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Clone a repository and keep it synchronized →