Back to Blog
Lesson 22 of the Git & GitHub: Git & GitHub from Zero course
GitAugust 9, 20264 min read

Linking Local Repositories: Connecting Git to GitHub

Learn how to create a GitHub repository and use git remote to link your local project to the cloud, enabling professional version control workflows.

gitgithubremoteversion controlterminal
Close-up of colorful programming code on a computer screen, showcasing digital technology.

Previously in this course, we covered the critical setup steps for secure interaction with cloud platforms, including Introduction to GitHub: Git vs. GitHub Explained and SSH Key Authentication: Secure Git Access for Beginners. Now that your environment is authenticated, we need to establish a formal connection between your local files and the remote world.

The Concept of a "Remote"

In Git, a "remote" is simply a pointer to a version of your repository hosted elsewhere—usually on a server like GitHub. While your local repository tracks the full history of your project, the remote serves as the central "source of truth" that allows for backup, collaboration, and deployment.

By convention, the first remote you add is named origin. Think of this as the "home base" for your project. Adding a remote does not move your files; it merely tells your local Git configuration where to send your data when you decide to share your progress.

Creating a Remote Repository on GitHub

Before running any commands in your terminal, you must create the "empty shell" on GitHub where your code will live.

  1. Log in to GitHub.
  2. Click the + icon in the top-right corner and select New repository.
  3. Give your repository a name (e.g., my-first-project).
  4. Important: Do not initialize the repository with a README, license, or gitignore at this step. Since you already have a local project initialized via Initializing a Repository: Start Your Git Project Today, you want to avoid conflicting history.
  5. Click Create repository.

GitHub will provide you with a screen containing a URL. Make sure you select the SSH option (which looks like git@github.com:username/repo-name.git) rather than the HTTPS option, as we configured your SSH Key Authentication in the previous step.

Adding a Remote Origin

With the repository created on GitHub, you are ready to link your local project. Open your terminal, navigate to your project directory, and use the git remote command.

Bash
# Add the remote repository
git remote add origin git@github.com:your-username/your-repository-name.git

How to Verify the Connection

To ensure the link was created successfully, you can list your remotes:

Bash
git remote -v

You should see output indicating that origin is mapped to your GitHub URL for both fetch and push operations.

ActionCommandPurpose
Addgit remote add origin <url>Links your local repo to the remote
Verifygit remote -vDisplays the current remote connections
Renamegit remote rename origin upstreamChanges the nickname of the remote
Removegit remote remove originUnlinks the remote connection

Hands-on Exercise

  1. Create the Remote: Follow the steps above to create an empty repository on your GitHub account.
  2. Link: Copy the SSH URL provided by GitHub and run git remote add origin <your-ssh-url> inside your local project folder.
  3. Confirm: Run git remote -v. You should see your URL listed twice (one for fetching, one for pushing).
  4. Clean up (Optional): If you made a typo, you can remove the remote using git remote remove origin and run the add command again.

Common Pitfalls

  • "Remote origin already exists": If you see this error, you likely added the remote previously. Use git remote -v to check, or if the URL is wrong, use git remote set-url origin <new-url> to correct it.
  • HTTPS vs. SSH: If you try to push later and it asks for a username/password, you likely used the HTTPS URL instead of the SSH URL. Always double-check your git remote -v output.
  • Initializing with README: If you accidentally checked the "Initialize this repository with a README" box on GitHub, your local and remote histories will diverge. You will have to pull that file down to your local machine before you can push your existing work.

Recap

Linking local repositories is the bridge between your private work and the global developer community. By creating a repository on GitHub and using git remote add origin to point your local terminal to that URL, you’ve prepared your project for its first synchronization.

Up next: Now that your local repository knows where its "home base" is, we’ll move your commits to the cloud using git push.

Similar Posts