How to Link to an Existing GitHub Repository

John Wachira Feb 02, 2024
How to Link to an Existing GitHub Repository

This article outlines the steps to link a local repository to a GitHub repository. Linking a local repository to an empty GitHub repo is always advisable.

After all, no one wants two different repositories in one. So, how do you link a local repo to a GitHub repo?

Linking to a GitHub repository is a relatively simple process. You will need to create a personal GitHub account.

For easier context, we will use an example.

In this example, we will get to our GitHub account and create a new empty repository called Sample-Repo. Ensure you do not include a README.md file or any other file suggested by GitHub.

If you do not know how to create a repository on GitHub, follow these steps.

  1. Go to your GitHub account and proceed to the Repositories tab.
  2. Click New to create a new repository. Give your repo a name and click Create repository.

Once done, we can now open the Git Bash. We will navigate to the local repository we want to link with our GitHub repository.

To be orderly, we have also named our local repository Sample-Repo.

We need to copy the GitHub URL for our Sample-Repo to link the two. In our case, we will use HTTPS authentication.

To get the link, go to the repository you created on GitHub, go to Code and copy the link under HTTPS.

We will run the git remote add command, as shown below.

$ git remote add origin https://github.com/Wachira11ke/Sample-Repo.git

This should set up a fetch and pull URL for our local repository. We can check this using the git remote -vv command, as shown below.

$ git remote -vv

Output:

git remote add

The last step involves pushing to the remote repository. We can use the git push command with the -u to push our master branch to the remote and track it.

We will run the command, as illustrated below.

$ git push -u origin master

Output:

git push -u origin master

The output above shows that Git has set our local master branch to track the remote master branch. At this point, we have successfully linked our local repo with an existing GitHub repo.

In a nutshell, you can link your local repo to a GitHub repo with the git remote add origin <URL>. Always use an empty GitHub repo to avoid mixing up your files.

Linking to an existing GitHub repo with other files can lead to errors.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn