Practice Remotes

Last updated: January 4, 2023

Remotes are instances of a repository on a network, on the web, on external drives… Departmental servers, USB keys, and version control repository hosting services like GitHub, GitLab, or Bitbucket can all serve as remotes.

In this course, we will use GitHub as the host for our remotes. If you haven't yet done so, create a free GitHub account.

Creating a project on GitHub

Your project (git_lesson ), though under version control, for now only resides on your computer. If you want to collaborate with others, having a remote on the web is really convenient.

Go to your GitHub account webpage, select the Repositories tab, then click the green New button.

Enter the name of your repo, avoiding spaces. It can be the name you had 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. In a real scenario, you would make it private if it contained sensitive information (e.g. unpublished research) or public if you wanted to make it available to others or to develop an open source project.

Here, we will go with the public option because, while GitHub free accounts allow private repositories, not all team options are available on private repositories for free accounts.

Now, you have this empty repository on GitHub, but it is not connected to your local repository. So let's go back to our local repository (so back to Git Bash or your terminal).

Managing remotes

List remotes

To list the remotes associated with your project, run:

git remote

And to see their address:

git remote -v

Since we haven't added any remote to our project yet, this should not return anything.

Add remotes

This is how you add a remote:

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

<remote-name> is the name you give to your remote. Usually, the name origin is used.

<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.

Example (using an SSH address):

git remote add origin git@github.com:<github-username>/<repo-name>.git

Example (using an HTTPS address):

git remote add origin https://github.com/<github-username>/<repo-name>.git

Practice

  • Add your new GitHub repo as the remote to your git_lesson project and call it origin
  • Run git remote -v now to see that you now have a remote

Rename remotes

You rename a remote with:

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

Remove remotes

And you delete a remote with:

git remote remove <remote-name>

Working with remotes

Pushing

You now have a remote, but it is empty. You need to push the branches of your local repo to update your remote with your project history:

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

Example:

git push origin master

…will push your master branch to your remote.

Practice

  • Run this, then refresh your GitHub repo page
  • You should see the content of your master branch
  • There is a branch drop down menu above your username. If you click on it, you will see that master is the only branch
  • Now pull your other branch to your remote and go make sure that it is there (you need to refresh the page)

You could use git push <remote-name> <branch-name> every time you want to push to your remote. But you can also associate a branch to a remote by pushing once with the flag --set-upstream :

git push –set-upstream <remote-name> <branch-name>

This will associate your local branch called <branch-name> with a branch of the same name in your remote.

After you have run this once, when you have checked out the branch <branch-name> , you simply need to run git push .

Practice

  • Associate master to your remote
  • Try to run git push from master
  • Check out another branch
  • Try git push (it shouldn’t work since you have not set any remote for that branch)
  • Associate that branch to your remote
  • Try git push again

Fetching

Now, if your collaborators also work on the project and push to it, you want to get their changes to your local repository:

git fetch <remote-name>

Example:

git fetch origin

This downloads the updates to your local repository, but it does not merge it with your work. You have to do this with git merge as we saw in the branch lesson.

Pulling

git pull fetches and merges in one command.

Comments & questions