# Remote repositories and how to use them

When you want to start sharing your work with the world or with other collaborators, you need to use remote repositories.

A remote repository is just a copy of the repository you've created locally, but hosted on an internet server.

There are a few websites that allow you to host your repositories:

  • GitHub (which is the most well-known one)
  • Bitbucket
  • GitLab

Each one has its benefits, such as the ability to mark your repositories as "private", so they're not open to the public. In addition, each one works slightly differently in how they handle interactions between collaborators so one might be more suitable for you and your style of working than others.

I use GitHub for the most part, so that's what I'll be teaching!

# Creating a remote repository

# Adding the remote to your local

Now that you have a remote repository, you can link it with your local repository by following the instructions GitHub leaves for you:

git remote add origin https://github.com/yourusername/your-repo.git

# Pushing changes

To push your commits to the repository, remember to first create commits in your repository and then add your remote.

Then, you can push to your branch using:

git push -u origin main  # or master

# Seeing changes in the remote

After you've made commits, GitHub will show you the latest code. You can also navigate to past commits.

# Pulling changes from the remote

If you have two computers working off the same remote repository (e.g. two people, or one person with two devices), eventually one of the devices will be out of sync with the remote.

This happens if one device makes a commit and pushes it to the remote.

The other device must now pull the changes from the remote, before making any more changes.

To do this:

git pull

# Cloning the remote repository

If you want to go to a fresh new device and download the entire remote repository, to set it up as a new local repository that you can work from, use git clone:

git clone https://github.com/yourusername/your-repo folder_name

This will download your_repo into a new folder called folder_name, and set it up as a Git repository so you can make further commits. The remote repository will already be set up so you can push your new commits.

If you want to clone a repository into the folder you're currently in, you can use . instead of a folder name:

git clone https://github.com/yourusername/your-repo .

Remember to leave a space between the URL and the .!