How To – Git: Add Local Repo to GitHub

This article makes the following assumptions:

  • There is an existing local installation of “git” with an existing local repository which does not yet exist on GitHub;
  • You already have a working GitHub account;
  • You either don’t have, or don’t want to use, “gh” (the GitHub CLI tool).

This will be accomplished in three steps:

  1. Create a completely empty repository with the desired name on GitHub;
  2. Add the remote URL to the existing local repository;
  3. Push the local updates to the remote GitHub repository.

Step 1 – Create empty target repo on GitHub

First of all sign in to your GitHub account using a web browser.

Next select “Home” from the top left “Hamburger” menu of GitHub and the click the green “New” button.

Give the new repository the same name as your existing local repository that you wish to “push” to it.

Give a brief single sentence description of the repository.

Choose either “Private” or “Public” for the scope of the new repository. My advice would be to initially make it “Private”. You can always make it “Public” later if you need to. The reason is that there is a well known vulnerability in GitHub which means that once a repository has been made public. There is a way for anyone who has previously connected to the repository to still gain access even if it is subsequently made private.

Make sure that the “Add a README file” check box is NOT checked to avoid conflicts with the first “push” command.

Set the “Add .gitignore” option to “None“. Again to avoid conflicts with the first “push” command.

Set the “Choose a license” option to “None“. You guessed it… to avoid conflicts with the first “push” command.

Click the green “Create repository” button at the bottom right of the form.

Step 2 – Add remote URL to the local repo

Go back to GitHub in your web browser and navigate to your new repository which will be completely empty.

Click on the “Down arrow” of the green “Code” button near to the top right of the page.

Click on the “SSH” tab of the dialogue that popped up.

Click on the “Copy url to clipboard” button to the right of the url text box. The url should be something along the lines of “git@github.com:gitUsername/name-of-repo.git”.

Switch back to a local command shell window and “cd” in to the root of the local repository directory structure.

Enter the following command:

git remote add origin url-pasted-from-clipboard

Obviously the contents of the clipboard should be used in place of the “url-pasted-from-clipboard” text. “origin” is a commonly used standard to use as a name for the remote GitHub host.

You can check that the remote URL has been correctly set by entering the following terminal command:

git remote -v

Step 3 – Push the local repo to GitHub

At the terminal command prompt enter the following command:

git push origin main

Here it is assumed that the convention of calling the primary branch “main” has been followed and that any required local commits have already been performed.

You can check the current status of your repository with respect to the GitHub version by entering the following terminal command from anywhere within the local repository directory hierarchy:

git status

Leave a Reply