Version Control with Git
Remotes in GitHub
- Explain what remote repositories are and why they are useful.
- Clone a remote repository.
- 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. Log in to GitHub, then click on the
+ icon in the top right corner to create a new repository called
<username>.github.io. Be sure to replace
<username> with your GitHub username.
Name your repository
<username>.github.io and then click “Create Repository”:
As soon as the repository is created, GitHub displays a page with a URL and some information on how to configure your local repository:
This effectively does the following on GitHub’s servers:
$ mkdir <username>.github.io $ cd <username>.github.io $ git init
Our local repository still contains the blog template and our earlier work on
_config.yml, but the remote repository on GitHub doesn’t contain any files yet:
The next step is to connect the two repositories. We do this by making the GitHub repository a remote for the local repository. The home page of the repository on GitHub includes the string we need to identify it:
Click on the ‘HTTPS’ link to change the protocol from SSH to HTTPS.
Copy that URL from the browser, go into the local
my-blog repository, and run this command:
$ git remote add origin https://github.com/<username>/<username>.github.io.git
We can check that the command has worked by running
git remote -v:
$ git remote -v
origin https://github.com/<username>/<username>.github.io.git (fetch) origin https://github.com/<username>/<username>.github.io.git (push)
origin is a local nickname for your remote repository: we could use something else if we wanted to, but
origin is by far the most common choice.
Once the nickname
origin is set up, this command will push the changes from our local repository to the repository on GitHub:
$ git push origin master
Counting objects: 44, done. Delta compression using up to 8 threads. Compressing objects: 100% (43/43), done. Writing objects: 100% (44/44), 1.48 MiB | 834.00 KiB/s, done. Total 44 (delta 7), reused 0 (delta 0) To https://github.com/<username>/<username>.github.io * [new branch] master -> master
Our local and remote repositories are now in this state:
We can pull changes from the remote repository to the local one as well:
$ git pull origin master
From https://github.com/<username>/<username>.github.io * branch master -> FETCH_HEAD Already up-to-date.
Pulling has no effect in this case because the two repositories are already synchronized. If someone else had pushed some changes to the repository on GitHub, though, this command would download them to our local repository.
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?