Remotes

Marie-Hélène Burle

training@westgrid.ca

What are Git remotes?

Remotes are repositories which serve as copies of a project and all of its history

They can be anywhere, including on an external drive or even on the same machine as the project

Often however, remotes are on a different machine to serve as backup or on a network (possibly the internet) to be accessible by several persons and serve as a syncing hub for collaborations

A number of online Git repository managers and hosting services have become popular remotes for Git projects. E.g.:

What are Git remotes?

A project can have several remotes

These remotes are characterized by an address (or a path if they are local) & identified by a name of your choice

Adding a GitHub remote

First, you need to create a new GitHub repository

Creating an empty repository on GitHub

Go to https://github.com , login, and go to your home page

Look for the Repositories tab, then click the green New button

Enter the name you want for your repo, without spaces

It can be the same name you have for your project on your computer (it would be sensible and make things less confusing), but it doesn’t have to be

You can make your repository public or private. Choose the private option if your research contains sensitive data or you do not want to share your project with the world. If you want to develop open source projects, of course, you want to make them public

Adding a GitHub remote

Click on the Code green drop-down button, select SSH (if you have set SSH for your GitHub account ) or HTTPS (if you haven’t) and copy the address

Then, go back to your command line, cd inside your project if you aren’t already there and add your remote

You add a remote with:

git remote add <remote-name> <remote-address>

<remote-name> is only a convenience name that will identify that remote. You can choose any name, but since Git automatically call the remote origin when you clone a repo, it is common practice to use origin as the name for the first remote

<remote-address> is the address of your remote in the https form or—if you have set SSH for your GitHub account —the SSH form

Adding a GitHub remote

Example (using an SSH address):
git remote add origin git@github.com:<user>/<repo>.git
Example (using an HTTPS address):
git remote add origin https://github.com/<user>/<repo>.git

Adding a GitHub remote

If you are working alone on this project and only wanted to have a remote for backup, you are set

If you don’t want to grant others write access to the project and only accept contributions through pull requests, you are also set

If you want to grant your collaborators write access to the project however, you need to add them to it

Inviting collaborators

  • Go to your GitHub project page
  • Click on the Settings tab
  • Click on the Manage access section on the left-hand side (you will be prompted for your GitHub password)
  • Click on the Invite a collaborator green button
  • Invite your collaborators with one of their GitHub user name, their email address, or their full name

Getting information on remotes

To list remotes, run:

git remote

To list the remotes with their addresses, run:

git remote -v

You can see that your local project now has a remote called origin and that it has the address of your GitHub repo

Getting information on remotes

To get yet more information about a particular remote, you can run:

git remote show <remote-name>
For instance, to inspect your new remote, run:
git remote show origin

Managing remotes

You rename a remote with:

git remote rename <old-remote-name> <new-remote-name>

You delete a remote with:

git remote remove <remote-name>

You can change the url of the remote with:

git remote set-url <remote-name> <new-url> [<old-url>]

Getting data from the remote

If you collaborate on your project through the GitHub remote, you will have to get the data added by your teammates to keep your local project up to date

To download new data from the remote, you have 2 options:

  • git fetch
  • git pull

Fetching changes

Fetching downloads the data from your remote that you don’t already have in your local version of the project.

git fetch <remote-name>

The branches on the remote are now accessible locally as <remote-name>/<branch>. You can inspect them or you can merge them into your local branches.

Example: To fetch from your new GitHub remote, you would run:
git fetch origin

Pulling changes

Pulling does 2 things: it fetches the data (as we just saw) and it then merges the changes onto your local branches.

git pull <remote-name> <branch>
Example:
git pull origin main

If your branch is already tracking a remote branch (see below), then you simply need to run:

git pull

Now, how do you upload data to the remote?

Pushing to a remote

Uploading data to the remote is called pushing and is done with:

git push <remote-name> <branch-name>

To push your branch main to the remote origin:

git push origin main

You can also set an upstream branch to track a local branch with the -u flag:

git push -u origin main

From now on, all you have to run when you are on main is:

git push

Git knows that your local main branch is being tracked by the upstream main branch

Questions?