7. Remotes and GitHub

Adding GitHub repository as a remote

Now that we have a GitHub repository, we need to connect it to our local repository. This will allow us to push our local changes to GitHub and pull changes made on GitHub into our local repository.

There are two common ways to work with remote repositories:

  1. New projects. You start locally, create a new repo with git init. When you've made some progress, connect the local repo to a remote to share your work with others (or to simply make a backup).

  2. Existing projects. Say, you bought a new computer, and you want to continue working on your existing project, which have been synced to a remote repository already. In this case, instead of git init, you use git clone https://your-repo-url to make a local copy of the remote repository. Then, work on it as you normally would.

We're following the first approach here: we already have a local repository with our work, and now we want to connect it to GitHub.

How do I do that?

To connect a local repository to GitHub, we need to add the GitHub repository as a remote in our local repository. A remote is essentially a bookmark that points to a URL where a repository is hosted.

The default name for the first remote is usually origin, but you can name it whatever you want. However, it's a convention to use origin for the primary remote repository.

To add a remote, we use the git remote add command, followed by the name of the remote and the URL of the repository. In our case, the command should look like this:

Run in Terminal:
git remote add origin https://github.com/your-name/your-repo.git
Task

Add the GitHub repository as a remote named origin for your local repository using the command above.

To confirm that the remote was added successfully, you can run git remote -v (-v is shorthand for --verbose, meaning show extra details), which will show you a list of all remotes and their URLs.

Run in Terminal:
git remote -v

You should see something like this:

Result:

origin https://github.com/your-name/your-repo.git (fetch) origin https://github.com/your-name/your-repo.git (push)

If you made a mistake in the URL, you can remove a remote with git remote remove <remote-name> (where <remote-name> is either origin or another name you used for your remote) and then add it again with the correct URL.

Task

Verify that the origin remote was added successfully.

Great! You've successfully connected your local repository to GitHub.

You can have multiple remotes for a repository, each pointing to a different URL. This is useful if you want to push to or pull from multiple repositories.
Next step
© 2024-2025 GitByBit.All rights reserved.

Hi! I'm Alex, creator of GitByBit.

This page is a part of the interactive course about Git version control.

It's a one-of-a-kind course that is integrated into the VS Code code editor. Learning directly in VS Code lets you operate Git exactly as you would in real life, doing real jobs, writing real code.

In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.

The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.

Learn Git in VS Code