Remotes
Remotes are copies of a project and its history.
They can be located anywhere, including on external drive or on the same machine as the project, although they are often on a different machine to serve as backup, or on a network (e.g. internet) to serve as a syncing hub for collaborations.
Popular online Git repository managers & hosting services:
Let’s see how to create and manage remotes.
Creating a remote on GitHub
Create a free GitHub account
If you don’t already have one, sign up for a free GitHub account.
Connect to GitHub
The 2 main ways to push data to GitHub are:
- using a token,
- using SSH.
We will use a token here.
There are alternative methods but you will have to look for information on your own:
- If you use Edge, there is an integration with GitHub since they are both owned by Microsoft.
- The GitHub Desktop application gives additional options to connect.
- If you use the GitHub app, it also gives authentication methods.
For information on how to use SSH with GitHub, you can look at this section of our course on collaborating with GitHub.
A token can be generated in GitHub settings.
Here is the workflow:
- In the upper-right corner of any page on GitHub, click your profile photo, then click Settings.
- In the left sidebar, click Developer settings.
- In the left sidebar, under Personal access tokens, click Fine-grained tokens.
- Click Generate new token.
- Under Token name, enter a name for the token.
- Under Repository access, select which repositories you want the token to access.
- Under Permissions, select which permissions to grant the token.
- Click Generate token.
- Make sure to copy the token and save it to your computer.
When you push to GitHub, you will be asked for your username and password. Your username is your GitHub username and the password is your token.
Create a repository on GitHub
- Go to the GitHub website, login, and go to your home page.
- Look for the Repositories tab & click the green New button.
- Enter the name you want for your repo, without spaces.
- Make the repository public or private.
Link remote repo to local repo
Click on the Code green drop-down button, select SSH if you have set SSH for your GitHub account or HTTPS and copy the address.
In the command line, cd
inside your project, and add the remote:
git remote add <remote-name> <remote-address>
remote-name
is a convenience name to 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.
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
Getting information on remotes
List remotes:
git remote
List remotes with their addresses:
git remote -v
Get more information on a remote:
git remote show <remote-name>
Example:
git remote show origin
Managing remotes
Rename a remote:
git remote rename <old-remote-name> <new-remote-name>
Delete a remote:
git remote remove <remote-name>
Change the address of a remote:
git remote set-url <remote-name> <new-url> [<old-url>]
Getting data from a remote
If you collaborate on a project, you have to get the data added by your teammates to keep your local project up to date.
To download new data from a remote, you have 2 options:
git fetch
git pull
Fetching changes
Fetching downloads the data from a 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:
git fetch origin
Pulling changes
Pulling fetches the changes & merges them onto your local branches:
git pull <remote-name> <branch>
Example:
git pull origin main
If your branch is already tracking a remote branch, you can omit the arguments:
git pull
Pushing to a remote
Uploading data to the remote is called pushing:
git push <remote-name> <branch-name>
Example:
git push origin main
You can set an upstream branch to track a local branch with the -u
flag:
git push -u <remote-name> <branch-name>
Example:
git push -u origin main
From now on, all you have to run when you are on main
is:
git push