Remotes in GitHub

Overview

Teaching: 30 min
Exercises: 0 min
Questions
  • How do I share my changes with others on the web?

Objectives
  • Explain what remote repositories are and why they are useful.

  • Push to or pull from a remote repository.

Version control really comes into its own when we begin to collaborate with other people. We already have most of the machinery we need to do this; the only thing missing is to copy changes from one repository to another.

Systems like Git allow us to move work between any two repositories. In practice, though, it’s easiest to use one copy as a central hub, and to keep it on the web rather than on someone’s laptop. Most programmers use hosting services like GitHub, BitBucket or GitLab to hold those master copies; we’ll explore the pros and cons of this in the final section of this lesson.

Let’s start by sharing the changes we’ve made to our current project with the world. At the top of GitHub Desktop, we see the words “Publish Repository”. Click on that, and it will give you a dialogue box. It might give you the option to keep the code private. Free accounts on GitHub only give a small number of private repositories, so we recommend using them judiciously.

Publish on GitHub

 

Once you have published the repository, you can push newer commits by clicking on Repository >> Push, or typing ctrl-P (command-P on a Mac).

Pulling allows you to bring commits from the remote repository into your local repository. This is accomplished by clicking Repository >> Pull, or typing shift-ctl-P. At the moment it won’t have any effect because the remote and local versions are synchronized. We’ll see what happens when they’re not in a little while.

GitHub Timestamp

Create a remote repository on GitHub. Push the contents of your local repository to the remote. Make changes to your local repository and push these changes. Go to the repo you just created on GitHub and check the timestamps of the files. How does GitHub record times, and why?

Solution

GitHub displays timestamps in a human readable relative format (i.e. “22 hours ago” or “three weeks ago”). However, if you hover over the timestamp, you can see the exact time at which the last change to the file occurred.

Push vs. Commit

In this lesson, we introduced the “push” command. How is “push” different from “commit”?

Solution

When we push changes, we’re interacting with a remote repository to update it with the changes we’ve made locally (often this corresponds to sharing the changes we’ve made with others). Commit only updates your local repository.

Key Points

  • A local Git repository can be connected to one or more remote repositories.

  • ‘push’ copies changes from a local repository to a remote repository.

  • ‘pull’ copies changes from a remote repository to a local repository.