Skip to content

Connecting a Local Repo to GitHub

You have a local Git repository with commits, and you have an empty GitHub repository waiting. Connecting them is a two-step process: tell your local repo where the remote is, then push your commits there.

A remote is a named reference to a repository stored somewhere other than your local machine — usually on GitHub, but it could be any Git-accessible location.

Most repositories have one remote, named origin by convention. The name “origin” is not special — it is just what GitHub’s instructions use, and it has become the standard. You could name it anything.

Terminal window
git remote add origin https://github.com/yourusername/git-practice.git

This registers the URL as a remote named origin in your local repository configuration. It does not move any files yet — it just saves the address so Git knows where to push and pull.

Terminal window
git remote -v

Output:

origin https://github.com/yourusername/git-practice.git (fetch)
origin https://github.com/yourusername/git-practice.git (push)

Two lines per remote — one for fetching (downloading) and one for pushing (uploading). Usually the same URL.

Push your commits to GitHub for the first time:

Terminal window
git push -u origin main

What each part means:

  • git push — send commits to the remote
  • -u — set the upstream: link your local main branch to origin/main so future git push and git pull commands know where to go without specifying the remote and branch name each time
  • origin — the remote to push to
  • main — the local branch to push

After this first push, future pushes from main only need:

Terminal window
git push

Git already knows where to push.

When you push for the first time, GitHub will ask you to authenticate. The method depends on your setup:

HTTPS with a Personal Access Token (PAT):

GitHub no longer accepts passwords over HTTPS. Instead:

  1. On GitHub: Settings → Developer settings → Personal access tokens → Tokens (classic) → Generate new token
  2. Give it a name and check the repo scope
  3. Copy the token — you will only see it once
  4. When Git prompts for a username, enter your GitHub username. For the password, paste the token.

Most systems (macOS Keychain, Windows Credential Manager, Git Credential Manager) will save the token so you only authenticate once.

SSH:

If you have set up SSH keys for GitHub, push commands authenticate automatically with no token required. SSH setup is a one-time configuration — instructions are in GitHub’s documentation under “Connecting to GitHub with SSH.”

Once the push completes, visit your GitHub repository in a browser. Refresh the page — your commits and files are now there, visible to anyone (if public) or just you (if private).

The branch, commit messages, file contents, and complete history are all visible through GitHub’s web interface.

After pushing, your git log --oneline shows:

b7e9d03 (HEAD -> main, origin/main) Add difficulty notes

origin/main is a remote-tracking branch — a local reference to where origin’s main branch was the last time you communicated with GitHub. As you make more local commits, main will advance ahead of origin/main until you push again.

  1. In your git-practice folder, connect the GitHub repository you created in the previous lesson:
Terminal window
git remote add origin https://github.com/yourusername/git-practice.git

(Replace yourusername with your actual GitHub username and use the URL GitHub showed you.)

  1. Verify the remote was added:
Terminal window
git remote -v
  1. Push to GitHub:
Terminal window
git push -u origin main

Authenticate if prompted.

  1. Visit your GitHub repository in a browser and confirm your commits and files are visible.

  2. Run git log --oneline and observe that origin/main now appears in the output, pointing to the same commit as main.

  • A remote is a named reference to a repository at another location. origin is the conventional name for a GitHub remote.
  • git remote add origin <url> registers the remote. It does not move any data.
  • git push -u origin main pushes commits to GitHub and sets the upstream so future pushes only need git push.
  • Authentication: HTTPS requires a Personal Access Token (not a GitHub password). SSH authenticates automatically with keys.
  • origin/main is a remote-tracking branch — a local reference to where GitHub’s main was last synced.

Next: pushing and pulling — the commands you use every day to keep your local and remote repositories in sync.