Linking GitHub to a local repo

In this tutorial, we will explore two scenarios.

Scenario 1

Create a new repo in GitHub

The new repo is now created.

Cloning proj2 onto your computer

The next step is to clone your GitHub repo onto your personal computer. The act of cloning creates an exact copy of the GitHub repository.

NOTE: As of August 2021, GitHub will no longer allow account password authentication when accessing a GitHub account using Git. Instead, you will be required to use personal access token or SSH based authentication. This page will be updated later this summer with token based instructions for connecting to GitHub with Git.

At this point, you should have a complete copy of the GitHub repo as a new folder. Note that you are not automatically placed into the new folder. Running ls -aF should show the proj2 folder.

Switch directory to proj2.

You'll note that Git added the .git hidden folder to the repo.

Scenario 2

In this scenario, we will create a new repo on GitHub, then connect that repo to an existing folder on our computer.

Create a new repo in GitHub

We adopt the same instructions listed in Scenario 1 to create a new GitHub repo, however, this time we will not have GitHub create the README file. This will ensure that the repo is completely empty. Name the new repo proj1. Note that we are choosing to assign it the same name as the project folder on our computer out of convenience, but the repo name does not need to match the project folder name.

Once created, we will be sent to a page with the .git link. Copy this link in your clipboard, you will need it in the next section.

The first step requires creating a handle in our local repo that will point to the GitHub repo.

In a Bash shell, navigate to your existing repo folder and type the following command:

This command creates an alias, origin, that, when called, will reference the GitHub repo.

Next, you will learn how to upload (aka push) your local repo to GitHub.

Push your local repo to GitHub

To push the current branch, main, to GitHub, type:

To push all local branches to GitHub, type:

Note that if you do not have a personal access token setup on your local computer, you might be prompted for a username and password (this option will only be available until August 2021).

Refresh your GitHub proj1 repo. It should now be populated with all the files and folders present in your local repo.

You can switch between branches on GitHub via the branch tab.

You won't see the .git/ folder on the GitHub repo, that's because GitHub has its own Git system. However, all versions and logs stored in your local .git/ folder do get synced with the GitHub repo.

This completes this part of the workshop. Next, you will explore different syncing scenarios between a local repo and a GitHub repo.

 

 


Back to the home page